谈一谈兼容Oracle和DB2开发时的注意事项

数据库 Oracle
本文主要介绍了兼容Oracle和DB2开发时的一些注意事项,了解了这些注意事项,我们在开发时就可以避免一些不兼容的现象的发生了。

我们在做兼容oralcedb2开发时,需要注意一些问题,以免遇到不兼容的现象,为开发者带来麻烦。本例子的前提是db2版本是9.7,且是开启PLSQL编译选项之后创建的数据库。接下来我们就开始介绍这些。

注意事项:

1. 在like 之后若使用了表字段,应统一改成使用locate函数,如:

oralce写法:

 

  1. select * from fw_right a where '03' like a.rightid||'%'; 

 

兼容写法:

 

  1. select * from fw_right a where locate('03',a.rightid) = 1; 

 

oralce写法:

 

  1. select * from fw_right a where '03' like '%'||a.rightid||'%'; 

 

兼容写法:

 

  1. select * from fw_right a where locate('03',a.rightid) > 0; 

 

2. 视图中使用的别名不应该与当前表字段同名。

如以下语句,在oracle中不会有问题,但在db2中会报"SQL0153N"错误。

 

  1. CREATE OR REPLACE VIEW V_WF_TODOLIST AS  
  2.  
  3. select c.process_def_id, c.process_def_name, a.action_def_id,  
  4.  
  5. a.work_item_id, a.bae007, a.action_def_name,  
  6.  
  7. a.state,  a.pre_wi_id,  a.work_type,  
  8.  
  9. a.operid, a.x_oprator_ids,  b.process_key_info,  
  10.  
  11. to_char(to_date(a.start_time, 'yyyymmddhh24miss'),'yyyy-mm-dd hh24:mi:ss') as start_time,  
  12.  
  13. to_char(to_date(a.complete_time,'yyyymmddhh24miss'),'yyyy-mm-dd hh24:mi:ss') as complete_time,  
  14.  
  15. a.filter_opr, a.memo,a.bae002,a.bae003, a.bae006,c.x_action_def_ids  
  16.  
  17. from wf_work_item a, wf_process_instance b, wf_action_def c  
  18.  
  19. where a.action_def_id  = c.action_def_id  
  20.  
  21. and b.process_def_id = c.process_def_id  
  22.  
  23. and a.bae007 = b.bae007  
  24.  
  25. and a.state in('0','2') 

 

兼容写法:

 

  1. CREATE OR REPLACE VIEW V_WF_TODOLIST AS  
  2.  
  3. select c.process_def_id, c.process_def_name, a.action_def_id,  
  4.  
  5. a.work_item_id, a.bae007, a.action_def_name,  
  6.  
  7. a.state,  a.pre_wi_id,  a.work_type,  
  8.  
  9. a.operid, a.x_oprator_ids,  b.process_key_info,  
  10.  
  11. to_char(to_date(a.start_time, 'yyyymmddhh24miss'),'yyyy-mm-dd hh24:mi:ss') as start_time_0,  
  12.  
  13. to_char(to_date(a.complete_time,'yyyymmddhh24miss'),'yyyy-mm-dd hh24:mi:ss') as complete_time_0,  
  14.  
  15. a.filter_opr, a.memo,a.bae002,a.bae003, a.bae006,c.x_action_def_ids  
  16.  
  17. from wf_work_item a, wf_process_instance b, wf_action_def c  
  18.  
  19. where a.action_def_id  = c.action_def_id  
  20.  
  21. and b.process_def_id = c.process_def_id  
  22.  
  23. and a.bae007 = b.bae007  
  24.  
  25. and a.state in('0','2') 

 

3.在下列情况下不允许ORDER BY 或 FETCH FIRST n ROWS ONLY:

  • 外层全查询视图
  •  "SQL 表函数"的 RETURN 语句中的外层全查询
  • 具体化查询表定义
  • 未用圆括号括起来的子查询

否则会报"SQL20211N  规范 ORDER BY 或 FETCH FIRST n ROWS ONLY 无效。"错误。

oralce写法:

 

  1. CREATE OR REPLACE VIEW V_FW_BLANK_BULLETIN as  
  2.  
  3. select id, bae001,  operunitid, operunittype, unitsubtype, ifergency,  
  4.  
  5. title,  content, digest,  duetime,  validto, aae100,  
  6.  
  7. bae006, bae002,  bae003,  id as colid,  
  8.  
  9. substr(digest,1,20) as digest2  
  10.  
  11. from fw_bulletin  
  12.  
  13. where duetime <= to_char(sysdate,'yyyymmddhh24miss')  
  14.  
  15. and (to_char(validto) >= to_char(sysdate,'yyyymmddhh24miss') or validto is null)  
  16.  
  17. and aae100 ='1' 
  18.  
  19. order by ifergency desc, id desc,  duetime desc 

 

兼容写法:

 

  1. CREATE OR REPLACE VIEW V_FW_BLANK_BULLETIN as  
  2.  
  3. select * from (select id, bae001,  operunitid, operunittype, unitsubtype, ifergency,  
  4.  
  5. title,  content, digest,  duetime,  validto, aae100,  
  6.  
  7. bae006, bae002,  bae003,  id as colid,  
  8.  
  9. substr(digest,1,20) as digest2  
  10.  
  11. from fw_bulletin  
  12.  
  13. where duetime <= to_char(sysdate,'yyyymmddhh24miss')  
  14.  
  15. and (to_char(validto) >= to_char(sysdate,'yyyymmddhh24miss') or validto is null)  
  16.  
  17. and aae100 ='1' 
  18.  
  19. order by ifergency desc, id desc,  duetime desc) 

 

了解了以上Oracle、DB2开发时的注意事项,我们在做开发的时候就能够尽量避免一些不兼容得状况了。本文就介绍到这里,希望能对各位有所帮助。

【编辑推荐】

  1. 数据仓库的逻辑建模之星型模式
  2. 简单介绍一下SQL Profiler的过滤设置
  3. 使用SQLite扩展函数来定义自己的函数
  4. Oracle数据库Shared Pool优化过程详解
  5. 检测局域网电脑是否有安装SQL Server数据库
责任编辑:赵鹏 来源: CSDN博客
相关推荐

2010-08-06 15:27:14

DB2 batch u

2010-08-04 11:23:59

2010-08-17 16:24:32

IBM DB2数据库

2010-11-01 13:24:15

DB2数据导入

2011-03-14 17:51:04

IBMDB2数据库

2010-07-30 13:28:10

2010-07-29 13:56:32

2010-11-02 13:09:42

DB2性能优化

2010-08-09 09:05:41

DB2快照函数

2010-08-04 10:44:32

2010-08-31 10:53:18

DB2导入数据库

2010-08-03 17:00:29

DB2 batch u

2010-07-29 13:09:48

DB2 9.7 兼容

2010-08-19 14:11:29

DB2临时表

2011-03-03 15:16:43

DB2数据库迁移

2011-08-02 13:08:06

Oracle索引

2011-07-28 09:22:56

Oracle WDPOracle数据库

2010-07-30 10:11:05

DB2临时表

2010-08-20 09:21:18

DB2打上补丁

2010-08-12 15:23:18

DB2数据迁移
点赞
收藏

51CTO技术栈公众号