db2 mysql兼容_DB2 V9.7 - Oracle兼容性问题

【题记】

今天处理了一个奇怪的DB2问题,描述如下:

用户提出在一个DB2 V9.7数据库中,查询dual伪表的current date结果竟然是datetime型,而不是date型,影响了业务查询结果。

接到问题后,立即登录数据库,执行查询 :

db2 "select current date from sysibm.dual"

返回结果:

-----------------------

2014-08-15-10.53.35

db2 "select current date from sysibm.sysdummy1"

返回结果:

-----------------------

2014-08-15-10.53.37

但是,其他DB2 V9.7版本DB2都返回date型:

------------

08/15/2014

【分析】

dual伪表来自Oracle,和DB2的sysibm.sysdummy1类似。这里想到了是否设置了Oracle兼容性?

于是和其他库进行对比:这个库的环境变量、db cfg等配置。

1、首先发现db2set环境变量设置了:DB2_COMPATIBILITY_VECTOR=ORA

2、再对比db cfg,发现与Oracle兼容性相关的配置开关都打开了:

Number compatibility = ON

Varchar2 compatibility = ON

Date compatibility = ON

正是这个db cfg变量"date_compat"打开了,才会出现查询的date型,变成了Oracle的datetime型。

至此,原因已经找到:

该DB2数据库开启了Oracle兼容性(DB2_COMPATIBILITY_VECTOR=ORA),并开启了date_compat配置(Date compatibility = ON)。

【注意】

1、只有在db2set DB2_COMPATIBILITY_VECTOR=ORA的情况下,新建的数据库,才会开启date_compat兼容性。

(不支持db2 update db cfg for using date_compat ON 的方式)

2、对于一个存在的DB2数据库,设置了db2set DB2_COMPATIBILITY_VECTOR=ORA之后,再db2stop、db2start,并不能开启date_compat兼容性。

(backup、restore的方式也不行)

【备注1】IBM Knowledge Center的解释:

Usage

The DB2_COMPATIBILITY_VECTOR registry variable is set and updated using the db2set command. Set the DB2_COMPATIBILITY_VECTOR before creating your database.When the DB2_COMPATIBILITY_VECTOR registry variable is set, all databases created should be created as UNICODE databases.

A new setting for the registry variable does not take effect until after the instance has been stopped and then restarted. Existing DB2 packages must be rebound for the change to take effect; packages that are not rebound explicitly will pick up the change on the next implicit rebind.

Example 1

This example sets the registry variable to enable all of the supported Oracle compatibility features:

db2set DB2_COMPATIBILITY_VECTOR=ORA

db2stop

db2start

Example 2

This example shows how to disable all compatibility features by resetting the DB2_COMPATIBILITY_VECTOR registry variable:

db2set DB2_COMPATIBILITY_VECTOR=

db2stop

db2start

Note that if a database has been created with the NUMBER data type or the VARCHAR2 data type enabled, use of the DATE data type as TIMESTAMP(0) enabled, or the creation of Oracle data dictionary-compatible views enabled, the database will still be enabled for these features after this db2set command executes.

【备注2】dbforums的讨论:

【最后】解决这个问题的办法:

应用程序的SQL改成如下,即可:   select varchar_format(current_date,'YYYY-MM-DD') from sysibm.dual ;

所有的DB2数据库都返回统一格式:2014-08-15

或者   select varchar_format(current_date,'MM/DD/YYYY') from sysibm.dual ;   返回:08/15/2014

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值