Db2 CLP下打开关闭自动提交(autocommit)选项

环境

  • Ubuntu 22.04
  • Db11.5.0

CLP (Command Line Processor)

使用 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.

CLP选项

使用 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 环境变量
  • 命令行flag(即 -c-f 等选项值)

可以使用 UPDATE COMMAND OPTIONS 命令来改变选项的设置,有两种用法:

  • 在交互模式下
  • 在批处理模式下

可见,在命令模式下,无法改变选项的设置(只能使用命令行flag指定选项值)。

自动提交(autocommit)

说了半天,终于说到正题了。

从上面的选项设置可见,自动提交的选项设置是 -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.

参考

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值