as400数据移植到oracle,SYBASE 数据库迁移到AS 400 db2的FAQ(一)-数据库专栏,SYBASE

笔者通过从事数据库的开发和管理工作数年!前不久刚刚完成sybase 数据库向db2数据库的迁移项目工作!

应网友和一些同学朋友的要求!要我介绍一些数据库迁移的策略和方法!

目前见于网上数据库迁移的资料较少!我实际工作中的遇到的技术难点和解决的策略和方法,简单介绍!希望对想要迁移数据库,又不知道如何下手的朋友有所借鉴!本文列出51个实际中问题和解决的策略和方法!非常注重实际!

1.sybase数据类型和db2有那些不同?

答:datetime  对应 timestamp

tinyint ,smallint  对应  smallint

money 类型对应 numeric(19,4)

2.db2数据库是否支持自增加列设置?

答:支持,例如:sysid  bigint generated always as identity

将原来sysid 的  numerical(18,0) 改为 bigint类型

3.db2是否有convert()函数,sybase 下这样语句如何转换?

答:convert(datetime,convert(char(8),dateadd(day,-1,getdate()),112))

db2下没有convert()函数,关于转换可以使用如下函数

char(),timestamp(),date(),time() …另外可以使用系统内部的系统变量

current timestamp , current date , current time 代表系统当前日期时间

如上语句可以这样转换

timestamp( current date –1 days,time(’00.00.00’))

4.sybase的isnull(@vc_pici_id,’0’) 如何转换?

答:db2下使用value(vc_pici_id,’0’)或者coalesce(vc_pici_id,’0’)

5.db2下如何调用存储过程和调用代返回值得存储过程?

答: (1)无返回参数调用call proc1(v_empno, v_salary)

(2)有返回参数调用

declare ret_var integer  default 0;

call proc1(var1,var2) ;

get diagnostics ret_var = return_status;

———

declare err_code integer default 0;

call   proc1(var1,err_code);

if err_code  = 1 then

xxxx;

end if;

6.sybase 下游标控制是非常方便的,使用sqlcode,sqlstate来控制,不知道db2下游标是如何控制的?

答:db2下游标控制不是非常的轻松和方便的,同样也可以使用sqlcode,sqlstate,或者用户自己控制,db2下sqlcode,sqlstate不能直接使用,必须声明后使用,(也就是说将系统的sqlcode,sqlstate本地实例化一分拷贝)。一般采用用户定义游标开关和sqlcode返回信息一起共同控制的方法.

举例1:(这里说明一个问题,游标开关是和sqlcode捆绑的。‘02000’就是sqlcode号)

//——-

标准使用游标的例子

标准while do 控制游标

//——-

create procedure creditp

(in i_perinc decimal(3,2),

inout o_numrec decimal(5,0))

language sql

begin                                      — 这里是用户管理事务

declare proc_cusnbr char(5);

declare proc_cuscrd decimal(11,2);

declare numrec decimal(5,0);

declare at_end int default 0;              — 开关定义

declare not_found  condition for 02000;  — 没有数据,游标结尾定义

declare c1 cursor for select cusnbr, cuscrd  from ordapplib.customer;

declare continue handler for not_found set at_end = 1;        –定义continue 条件

declare exit handler for sqlexception     rollback ;          –sqlcode 非010002则退出并回滚事务

set numrec = 0;

open c1;

fetch c1 into proc_cusnbr, proc_cuscrd;

while at_end = 0 do

set proc_cuscrd = proc_cuscrd +(proc_cuscrd * i_perinc);

set numrec = numrec + 1;

fetch c1 into proc_cusnbr, proc_cuscrd;

end while;

set o_numrec = numrec;

close c1;

commit;           –提交事务

end

举例2:

–声明游标c1

declare c1 cursor for

select cusnbr, cuscrd

from ordapplib.customer;

open c1; –打开游标

fetch c1 into proc_cusnbr, proc_cuscrd; –从游标获取数据

if sqlstate = 02000 then  –判断游标是否有数据(无)

call data_not_found;    –返回调用

else

do while (substr(sqlstate,1,2) = 00 | substr(sqlstate,1,2) = 01);

fetch c1 into proc_cusnbr, proc_cuscrd;

……

end if ;

close c1;  –关闭游标

7.db2存储过程的标准格式如何?

答:db2存储过程的编写比较灵活,但是语法格式是非常严格的。基本如下

cteate  procedure  xhzq_db.procname (

in  para_1  type  default  value ,

out para_2 type  default  value)

begin   ( 用户管理事务)

–用户定义变量

declare   var1  type  default  value;

–用户定义控制临时变量

declare   error_code int default 0;

declare   at_end  int  default 0;

— 用户定义条件控制变量

declare not_found  condition for 02000;

— 用户定义游标

declare  c1  cursor  for  select  col  from   table_name ;

— 用户定义条件和开关变量关联

declare continue handler for not_found set at_end = 1;

— 用户定义事务回滚处理条件

declare exit handler for sqlexception     rollback ;

— 初始化变量直

set  var  = 0;

— 判断入口参数

if (para_1 is null) then

set  para =  100;

end if;

— 存储过程语句体集合

open  c1 ;

fetch  c1 into  xx,xx2,xx3  ;

….

close  c1;

— 提交事务

commit work hold;

set para_2 = 0 ;

return  para_2 ;

— 返回参数0 成功

end

8.db2下char()函数用法

答: char(date,keyword)  , char(time,keyword)

keyword

date format

time format

usa

07/17/2001

01:48pm

iso

2001-07-17

13.48.04

eur

17.07.2001

13.48.04

jis

2001-07-17

13.48.04

select current date,current time from sysibm>sysdummy1;

9.db2下timestamp()函数用法

答:timestampdiff(k,char(ts1 – ts2))

k

date part

k

date part

256

years

128

quarters

64

months

32

weeks

16

days

8

hours

4

minutes

2

seconds

10.db2下如何进行字符串组合拼接?

答:使用‘||’处理,concat()函数

例如: declare   vc_sql_str   vchar(100);

declare   vc_where    vchar(100);

declare   vc_from     vchar(100);

set vc_from = ‘ from xhzq_db.sys_parameter_tab ‘;

set vc_where = ‘ where vc_id is null ‘;

set  vc_sql_str = ‘ select * ‘ || vc_from || vc_where  ;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值