PostgreSQL sql放入文件批量执行
2015-03-18 11:00
471 查看
PostgreSQL sql放入文件,登入数据库之后批量执行
1. 建立测试sql:
vi aa.sql
插入:猜测每条sql语句是用;分隔的,function中的多个;也会自动识别。
create table tb1(id integer);
insert into tb1 select generate_series(1,10);
select * from tb1;
delete from
tb1 where id<3;
select * from tb1;
2. 将aa.sql放入 ./src/postgresql-9.3.5/src/tutorial下([b]./src/postgresql-9.3.5/src/tutorial是PostgreSQL自动识别的目录,当然也可以放在任意目录,比如/home/postgres/aa.sql)[/b]
3. 切换用户登入
su postgres
psql postgres
4. 执行:当输入\i时候,会自动检测到./src/postgresql-9.3.5/src/tutorial下的文件,PostgreSQL的测试例子也放在此目录下
第二个例子:
vi bb.sql:
写入一个function:
执行前:
执行后:
5. 也可以使用psql命令执行
1. 建立测试sql:
vi aa.sql
插入:猜测每条sql语句是用;分隔的,function中的多个;也会自动识别。
create table tb1(id integer);
insert into tb1 select generate_series(1,10);
select * from tb1;
delete from
tb1 where id<3;
select * from tb1;
2. 将aa.sql放入 ./src/postgresql-9.3.5/src/tutorial下([b]./src/postgresql-9.3.5/src/tutorial是PostgreSQL自动识别的目录,当然也可以放在任意目录,比如/home/postgres/aa.sql)[/b]
3. 切换用户登入
su postgres
psql postgres
4. 执行:当输入\i时候,会自动检测到./src/postgresql-9.3.5/src/tutorial下的文件,PostgreSQL的测试例子也放在此目录下
postgres=# \i aa.sql (\i /home/postgres/aa.sql) id | name ----+------ 1 | join 2 | join 3 | join 4 | join 5 | join 6 | join 7 | join 8 | join 9 | join 10 | join (10 rows) CREATE TABLE INSERT 0 10 id ---- 1 2 3 4 5 6 7 8 9 10 (10 rows) DELETE 2 id ---- 3 4 5 6 7 8 9 10 (8 rows) postgres=#
postgres=# \d tb1 Table "public.tb1" Column | Type | Modifiers --------+---------+----------- id | integer |
第二个例子:
vi bb.sql:
写入一个function:
create function func1()returns void as $$ declare begin delete from person where id>5; delete from tb1 where id>5; end $$language plpgsql; select func1();切换到postgres,登入之后执行:
执行前:
postgres=# select * from person ; id | name ----+------ 1 | join 2 | join 3 | join 4 | join 5 | join 6 | join 7 | join 8 | join 9 | join 10 | join (10 rows) postgres=# select * from tb1 ; id ---- 3 4 5 6 7 8 9 10 (8 rows)执行:
postgres=# \i bb.sql CREATE FUNCTION func1 ------- (1 row)
执行后:
postgres=# select * from person ; id | name ----+------ 1 | join 2 | join 3 | join 4 | join 5 | join (5 rows) postgres=# select * from tb1 ; id ---- 3 4 5 (3 rows) postgres=#
5. 也可以使用psql命令执行
pslq -d postgres -U postgres -f /home/postgres/aa.sql
相关文章推荐
- 批量执行特定文件目录下SQL文件脚本
- 在windows环境下批量执行ORACLE的*.sql文件
- sqlite数据库,进行批量执行.sql文件的时候,会出现一个:not an error的错误提示
- java调用sqlplus执行批量脚本获得输出结果和调用.bat文件
- postgresql导出sql执行结果到文件的方法
- 批量执行SQL文件
- 批量执行SQL文件
- 在数据库中批量执行SQL并将结果记录到文件中
- oracle批量执行sql文件的方法
- 读取一个SQL文件 + 批量执行batchUpdate(String[ ])
- 往DB2导入.sql文件(表结构),批量执行SQL文建表
- 怎么用批处理*.bat 执行文件目录下面的SQL语句脚本(批量的)。。。@echo off是什么意思?
- 批量执行SQL文件
- windows下批量执行sql文件
- 往DB2导入.sql文件(表结构),批量执行SQL文建表
- 在windows环境下批量执行ORACLE的*.sql文件
- 批量执行sql文件并输出执行日志
- Oracle批量执行SQL文件
- java程序连接数据库并批量执行.sql文件
- oracle批量执行文件脚本sql