使用 db2
命令启动CLP。根据命令选项可分为:
不加任何命令选项,直接输入 db2
并回车,则进入交互模式,其提示符为 db2 =>
。在交互模式下,可以直接运行命令,例如:
~$ db2
(c) Copyright IBM Corporation 1993,2007
Command Line Processor for DB2 Client 11.5.0.0
You can issue database manager commands and SQL statements from the command
prompt. For example:
db2 => connect to sample
db2 => bind sample.bnd
For general help, type: ?.
For command help, type: ? command, where command can be
the first few keywords of a database manager command. For example:
? CATALOG DATABASE for help on the CATALOG DATABASE command
? CATALOG for help on all of the CATALOG commands.
To exit db2 interactive mode, type QUIT at the command prompt. Outside
interactive mode, all commands must be prefixed with 'db2'.
To list the current command option settings, type LIST COMMAND OPTIONS.
For more detailed help, refer to the Online Reference Manual.
db2 => connect to sample
Database Connection Information
Database server = DB2/LINUXX8664 11.5.0.0
SQL authorization ID = DB2INST1
Local database alias = SAMPLE
db2 => select * from sysibm.sysdummy1
IBMREQD
-------
Y
1 record(s) selected.
db2 => connect reset
DB20000I The SQL command completed successfully.
db2 => quit
DB20000I The QUIT command completed successfully.
正如上面提示信息所说,在命令模式下,所有命令都必须加上 db2
前缀。例如:
~$ db2 connect to sample
Database Connection Information
Database server = DB2/LINUXX8664 11.5.0.0
SQL authorization ID = DB2INST1
Local database alias = SAMPLE
~$ db2 "select * from sysibm.sysdummy1"
IBMREQD
-------
Y
1 record(s) selected.
~$ db2 connect reset
DB20000I The SQL command completed successfully.
批处理模式和命令模式类似,只不过使用 -f
选项指定一个输入文件,例如,文件 temp.sql
内容如下:
connect to sample;
select * from sysibm.sysdummy1;
connect reset;
使用 db2 -tvf temp.sql
来运行该文件:
~$ db2 -tvf temp.sql
connect to sample
Database Connection Information
Database server = DB2/LINUXX8664 11.5.0.0
SQL authorization ID = DB2INST1
Local database alias = SAMPLE
select * from sysibm.sysdummy1
IBMREQD
-------
Y
1 record(s) selected.
connect reset
DB20000I The SQL command completed successfully.
使用 LIST COMMAND OPTIONS
命令来查看CLP选项:
~$ db2 list command options
Command Line Processor Option Settings
Backend process wait time (seconds) (DB2BQTIME) = 1
No. of retries to connect to backend (DB2BQTRY) = 60
Request queue wait time (seconds) (DB2RQTIME) = 5
Input queue wait time (seconds) (DB2IQTIME) = 5
Command options (DB2OPTIONS) =
Option Description Current Setting
------ ---------------------------------------- ---------------
-a Display SQLCA OFF
-b Auto-Bind ON
-c Auto-Commit ON
-d Retrieve and display XML declarations OFF
-e Display SQLCODE/SQLSTATE OFF
-f Read from input file OFF
-i Display XML data with indentation OFF
-j Return code for system calls OFF
-l Log commands in history file OFF
-m Display the number of rows affected OFF
-n Remove new line character OFF
-o Display output ON
-p Display interactive input prompt ON
-q Preserve whitespaces & linefeeds OFF
-r Save output to report file OFF
-s Stop execution on command error OFF
-t Set statement termination character OFF
-v Echo current command OFF
-w Display FETCH/SELECT warning messages ON
-x Suppress printing of column headings OFF
-y Get SQL message text from server ON
-z Save all output to output file OFF
可以通过两种方式来使用CLP选项:
DB2OPTIONS
环境变量-c
、 -f
等选项值)可以使用 UPDATE COMMAND OPTIONS
命令来改变选项的设置,有两种用法:
可见,在命令模式下,无法改变选项的设置(只能使用命令行flag指定选项值)。
说了半天,终于说到正题了。
从上面的选项设置可见,自动提交的选项设置是 -c
,其默认值是 ON
,也就是自动提交。
-c
+c
或者 -c-
前面提到,在交互模式下,可以通过 UPDATE COMMAND OPTIONS
命令来改变选项设置。
update command options using c on
update command options using c off
下面看一个具体的例子:
db2 => select * from t2
C1 C2
----------- -----------
1 111
1 record(s) selected.
db2 => update command options using c off
DB20000I The UPDATE COMMAND OPTIONS command completed successfully.
db2 => update t2 set c2 = 222
DB20000I The SQL command completed successfully.
db2 => select * from t2
C1 C2
----------- -----------
1 222
1 record(s) selected.
db2 => rollback
DB20000I The SQL command completed successfully.
db2 => select * from t2
C1 C2
----------- -----------
1 111
1 record(s) selected.
貌似 UPDATE COMMAND OPTIONS
命令在命令模式下不起作用。
$ db2 update command options using c off
DB20000I The UPDATE COMMAND OPTIONS command completed successfully.
但是查看选项,它还是 ON
:
~$ db2 list command options | grep -i commit
-c Auto-Commit ON
在命令模式下,只能每个命令都加上 +c
选项,来指定关闭自动提交:
~$ db2 "select * from t2"
C1 C2
----------- -----------
1 111
1 record(s) selected.
~$ db2 +c "update t2 set c2 = 222"
DB20000I The SQL command completed successfully.
注意:update操作并没有提交。
~$ db2 +c "select * from t2"
C1 C2
----------- -----------
1 222
1 record(s) selected.
注意:这里一定别忘了加上 +c
,表示和上一条命令在同一个事务里,且都没提交。若没有加 +c
,则本命令和上一条命令在同一事务里,本条命令运行结束后,提交事务。
~$ db2 rollback
DB20000I The SQL command completed successfully.
~$ db2 "select * from t2"
C1 C2
----------- -----------
1 111
1 record(s) selected.
和交互模式类似,通过 UPDATE COMMAND OPTIONS
命令来改变选项设置。
文件 temp.sql
内容如下:
connect to sample;
select * from t2;
update command options using c off;
update t2 set c2 = 222;
select * from t2;
rollback;
select * from t2;
connect reset;
使用 db2 -tvf temp.sql
来运行该文件:
~$ db2 -tvf temp.sql
connect to sample
Database Connection Information
Database server = DB2/LINUXX8664 11.5.0.0
SQL authorization ID = DB2INST1
Local database alias = SAMPLE
select * from t2
C1 C2
----------- -----------
1 111
1 record(s) selected.
update command options using c off
DB20000I The UPDATE COMMAND OPTIONS command completed successfully.
update t2 set c2 = 222
DB20000I The SQL command completed successfully.
select * from t2
C1 C2
----------- -----------
1 222
1 record(s) selected.
rollback
DB20000I The SQL command completed successfully.
select * from t2
C1 C2
----------- -----------
1 111
1 record(s) selected.
connect reset
DB20000I The SQL command completed successfully.