假設:
A.數據庫servername: testserver
B.數據庫名:testdb
C.SQL腳本文件:
sqlfile.sql
create table test_table(c1 integer);
insert into test_table(c1) values(12);
update test_table set c1=0;
delete test_table where 1=1;
drop table test_table;
sqlfile2.sql
create table test_table(c1 integer);
insert into test_table(c2) values(12);
insert into test_table(c1) values(12);
update test_table set c1=0;
delete test_table where 1=1;
drop table test_table;
一.執行SQL腳本文件方式
1.調用dbaccess執行SQL腳本文件
A.簡單執行SQL文件
dbaccess testdb sqlfile.sql 或者 dbaccess testdb@testserver sqlfile.sql
Database selected.
Table created.
1 row(s) inserted.
1 row(s) updated.
1 row(s) deleted.
Table dropped.
Database closed.
B.執行輸出打印執行語句及執行結果
dbaccess -e testdb sqlfile.sql
create table test_table(c1 integer);
Table created.
insert into test_table(c1) values(12);
1 row(s) inserted.
update test_table set c1=0;
1 row(s) updated.
delete test_table where 1=1;
1 row(s) deleted.
drop table test_table;
Table dropped.
Database closed.
C.將執行結果重定向到文件(shell: bash)
dbaccess -e testdb sqlfile.sql > output.log > 2&1
cat output.log
create table test_table(c1 integer);
Table created.
insert into test_table(c1) values(12);
1 row(s) inserted.
update test_table set c1=0;
1 row(s) updated.
delete test_table where 1=1;
1 row(s) deleted.
drop table test_table;
Table dropped.
Database closed.
二.交互模式執行語句
dbaccess - -
> database testdb;
Database selected.
> create table test_table(c1 integer);
Table created.
> insert into test_table(c1) values(12);
1 row(s) inserted.
> update test_table set c1=0;
1 row(s) updated.
> delete test_table where 1=1;
1 row(s) deleted.
> drop table test_table;
Table dropped.
>^D
三.菜單模式執行
進入數據庫
dbaccess 選擇數據庫
或者
dbaccess testdb
dbaccess testdb@testserver
按菜單操作執行SQL語句,其中可以使用vi進行腳本文件的編輯
四.幾個小技巧
1.執行SQL腳本文件時,當出錯時打印詳細的錯誤信息
dbaccess -e -m testdb sqlfile2.sql
Database selected.
create table test_table(c1 integer);
Table created
insert into test_table(c2) values(1212);
217: Column (c2) not found in any table in the query (or SLV is undefined).
Error in line 2
Near character position 26
insert into test_table(c1) values(12);
1 row(s) inserted.
update test_table set c1=0;
1 row(s) updated.
delete test_table where 1=1;
1 row(s) deleted.
drop table test_table;
Table dropped.
Database closed.
2.執行SQL腳本文件時,當出錯時打印詳細的錯誤信息並在出現第一個錯誤時,停止繼續執行后續的語句
dbaccess -e -m -a testdb sqlfile2.sql
Database selected.
create table test_table(c1 integer);
Table created.
insert into test_table(c2) values(1212);
217: Column (c2) not found in any table in the query (or SLV is undefined).
Error in line 2
Near character position 26
Database closed.