欢迎投稿

今日深度:

数据库db2到oracle迁移解决方案,

数据库db2到oracle迁移解决方案,


数据库db2到oracle迁移解决方案

前言

    本文对数据库db2到oracle迁移过程遇到的问题和解决方案进行总结梳理,为项目从db2数据库迁移到oracle数据库sql语句修改提供参考。

1.取前n条数据

描述:

    db2语法为fetch first n rows only,oracle取前n条数据的语法为在where条件中增加rownum<=n;

举例:

1--按照f_id降序取表tab_example_info中前十条数据
2--db2
3select * from tab_example_info order by f_id desc fetch first 10 rows only
4--oracle
5select * from (select * from tab_example_info order by f_id desc) where rownum<=10

2.rownum和rowid

描述:

    oracle含有rowid ,由数据库唯一产生的,可以在程序里可以获得;db2不能被程序获得。

    oracle中含有rowid和rownum,其中rownum是获取查询结果集后再加上去的,rowid是数据库数据产生的时候生产的结果。

举例:

1--oracle rownum和rowid
2--1.rownum只能用< 或者<=
3--2.rownum<或者<=今年放在子查询里面
4--错误用法
5select rownum,f_name from tab_example_info where rownum>5 and rownum<10;
6--正确用法
7select * from (select rownum,f_name from tab_example_info) where rownum<10;
8--rowid用法
9select rowid,phone_no from tab_example_info where rowid>5 and rowid<10;

 

3.空置转换

描述:

    当select语句选出的字段为NULL时,可以使用函数转换成默认值,oracle中的函数为NVL,db2中的使用的函数为value。

举例:

1--db2
2select f_id,f_name,value(f_rate,'0') from eg.tab_example_info;
3--oracle
4select f_id,f_name,nvl(f_rate,'0') from tab_example_info;

 

4.空字符串处理

描述:

        oracle认为f_name=’’为false,当where f_name=’’出现时,select、update、delete无法匹配到结果集。

举例:

1--%s为字符串占位符,%s有可能是空串,查询条件f_name='%s'
2--db2
3select f_id from tab_example_info where f_name='%s';
4--oracle
5select f_id from tab_example_info where (f_name=’%s’ or (f_name is null and ‘%s’ is null));

 

5.where条件弱类型判断

描述:

     oracle中[where 字符型字段 in (整形) ]是允许,但db2不允许;oracle[where 字符型字段=数字型字段]允许,但db2不允许

举例:

1--oracle可通过,db2中报错
2select 'abc' from tab_example_info where '1' in (1) 
3--oracle可通过,db2中报错
4select 'abc' from tab_example_info where '1'=1 

6.类型转换

描述:

    oracle和db2数据类型转换特殊写法与兼容写法。

 

 1--Oracle:
 2--整型转字符型,字符串转整型
 3to_char(1),to_char(1.1),to_number('1'),to_number('1.1')
 4to_data('2018-10-27 19:17:30','YYYY-MM-DD HH24:MI:SS')
 5to_char(to_date('2018-10-27','yyyy-mm-dd'),'yyyy-mm-dd')
 6--db2
 7char(1),int('1'),double('1.1'),char(1.1)date('2018-10-27')
 8to_date('2018-10-27 19:17:30','YYYY-MM-DD HH24:MI:SS')
 9char(date('2018-10-27'))
10--兼容写法
11cast  (1 as char)
12cast  ('1' as int)

7.时间和日期

描述:

获取当前时间:

    oracle Sysdate

    CURRENT DATE

日期:

    oracle中DATE型也是带有时分秒的,db2下DATE只是年月日,如'2018-10-27',且可作为字符串直接操作。db2中要记录时分秒必须采用TIMESTAMP型。

 

结尾

    本文对迁移过程做了简单梳理,如后续有补充会个人博客和

www.htsjk.Com true http://www.htsjk.com/teradata/36458.html NewsArticle 数据库db2到oracle迁移解决方案, 数据库db2到oracle迁移解决方案 前言     本文对数据库db2到oracle迁移过程遇到的问题和解决方案进行总结梳理,为项目从db2数据库迁移到oracle数据库sql语...
相关文章
    暂无相关文章
评论暂时关闭