| 订阅 | 在线投稿
分享
 
 
 

经验分享:Informix和Oracle存储过程的异同

2008-08-05 07:05:03 编辑來源:互联网 国际版 评论
 
 
本文为【经验分享:Informix和Oracle存储过程的异同】的汉字拼音对照版显示拼音
  zaigongzuoguochengzhongshejidaoguoyoushiyaojinxingINFORMIXheORACLEzhijiandeyizhiyoushiyingyongxuyaojizhichiINFORMIXshujukuyouyaozhichiORACLEshujukuruguoyingyongchengxushejidaocunchuguochengdehuajiuxuyaojishuxiINFORMIXcunchuguochengdexiefayouxuyaoshuxiORACLEcunchuguochengdexiefabizhejiangzaigongzuozhongzongjiedeyixiejingyanxiechulaiyudajiagongxiang

  jianlicunchuguochengdeyufa:

  yiInformix

  create procedure proc_name( [....in_parameter_list])

  returning out_para_list / out_result_set;

  erOracle

  create [or replace] procedure procedue_name

  [ (arg1 [ {in | out | in out }] type

  (argn [ {in | out | in out }] type,)]

  {is | as} --daitiDECLAREguanjianzi

  [ bianliangdingyiqu]

  begin

  end procedure_name;

  sanjigejiandandelizi

  1meiyoushenshuyemeiyoufanhuizhi

  1Informix

  create procedure pNoParam()

  begin

  on exception

  rollback work;

  return;

  end exception

  begin work;

  delete from t1;

  delete from t2;

  commit work;

  end;

  end procedure;

  2Oracle

  create or replace procedure pNoParam

  as

  begin

  delete from t1;

  delete from t2;

  commit;

  exception

  when others then

  begin

  rollback;

  end;

  end pNoParam;

  2youshurushuchu

  wangt1biaozhongcharuyitiaojilufanhuizhibiaoshicharushifouchenggong

  1Informix

  create procedure pNormalParam(f1 integer, f2 varchar(10))

  returning integer;

  begin

  on exception

  rollback work;

  return -1;

  end exception

  begin work;

  insert into t1 values(f1, f2);

  commit work;

  return 0;

  2Oracle

  create or replace procedure pNormalParam(f1 number,

  f2 varchar2, v_Result out number)

  as

  begin

  insert into t1 values(f1,f2);

  commit;

  v_Result = 0;

  return;

  exception

  when others then

  begin

  rollback;

  v_Result := -1;

  end;

  end pNormalParam;

  xuyaozhuyideshizaioraclecunchuguochengzhongshenshushibunengjiashangsizedebiruf1zait1biaozhonggaiziduanshinumber(10,0)erzhelizhinengxienumbererbunengxienumber(10,0)

  3fanhuijiluji

  1Informix

  create procedure pReturnSet() returning integer, varchar(10);

  define i integer;

  define j varchar(10);

  foreach

  select f1, f2 into i, j from t1

  return i, j with resume;

  end foreach;

  end procedure;

  2Oracle

  create or replace package TestRefCursorPkg as type TestRefCursorTyp is ref cursor; procedure pReturnSet(RefCursor out TestRefCursorTyp); end TestRefCursorPkg;

  create or replace package body TestRefCursorPkg as

  procedure pReturnSet (RefCursor out TestRefCursorTyp)

  as

  localCursor TestRefCursorTyp;

  begin

  open localCursor for select f1, f2 from t1;

  RefCursor := localCursor;

  end pReturnSet;

  end TestRefCursorPkg;

  /

  siqitachayishuoming

  1cuowubuzhuo

  1Informix

  使shiyong

  on exception

  end exception

  2Oracle

  使shiyong

  exception

  when others then

  2duiyoubiaodechuli

  1Informix

  create procedure pHasCursor()

  define v_f1 integer

  begin

  on exception

  rollback work;

  return;

  end exception

  begin work;

  foreach curt1 with hold for

  select f1 into v_f1 from t1 -- zhuyizhelimeiyoufenhao

  if (v_f1 = 1) then

  update t1 set f2 = 'one' where current of curt1;

  elif (v_f1 = 2) then

  update t1 set f2 = 'two' where current of curt1;

  else

  update t1 set f2 = 'others' where current of curt1;

  end if;

  end foreach;

  commit work;

  end

  end procedure;

  2Oracle

  create or replace procedure pHasCursor

  as

  v_f1 number(10,0);

  cursor curt1 is

  select f1 from t1 for update;

  begin

  open curt1;

  loop

  fetch curt1 into v_f1;

  exit when curt1%notfound;

  if (v_f1 = 1) then

  update t1 set f2 = 'one' where current of curt1;

  elsif (v_f1 = 2) then

  update t1 set f2 = 'two' where current of curt1;

  else

  update t1 set f2 = 'others' where current of curt1;

  end if;

  end loop;

  commit;

  return;

  exception

  when others then

  begin

  rollback;

  end;

  end pHasCursor;

  

  3zaicunchuguochengzhongtiaoyonglingwaiyigecunchuguocheng

  1Informix

  Call pNoParam();

  Call pNormalParam(1, a) returning v_Result;

  2Oracle

  pNoParam;

  pNormalParam(1, a, v_Result);

  4riqicaozuo

  1dangqianshijian

   Informix

  define cur_dtime_var datetime year to second;

  dangqianriqishijian let cur_dtime_var = current; -- datetime

   Oracle

  Currtime date;

  Currtime := sysdate;

  2dangqianriqidezengjian

   Informix

  let tmp_date = today + 3 UNITS day; -- dangqianshijianjiasantian

  let tmp_datetime = current + 1 UNITS second; -- dangqianshijianjia1miaozhong

   Oracle

  Tmp_date := sysdate + 3; -- dangqianshijianjiasantian

  Tmp_date := sysdate + 1/24/3600; --dangqianshijianjia1miaozhong

  3riqizhuanhuanchengzifuchuan

   Informix

  let v_PeriodEndTime = year(v_date)||extend(v_date,month to month)

  ||extend(v_date,day to day) ||extend(v_date,hour to hour)

  ||extend(v_date,minute to minute)|| extend(v_date,second to second);

   Oracle

  v_PeriodEndTime := to_char(v_date, 'yyyymmddhh24miss');

  4zifuchuanzhuanhuanchengriqi

  jiashezifuchuandexingshishiyyyymmddhhmissxingshide

   Informix

  -- zhijiezhuanhuanchengriqi

  let v_BeginDate = substr(v_BeginTime,1,4)||'-'||substr(v_BeginTime,5,2)

  ||'-'||substr(v_BeginTime,7,2)||' '||substr(v_BeginTime,9,2)

  ||':'||substr(v_BeginTime,11,2)||':'||substr(v_BeginTime,13,2);

  -- zhegeyuedediyitian

  let v_date = substr(v_BeginTime,1,4)||'-'

  ||substr(v_BeginTime,5,2)||'-1 00:00:00';

  -- zhegexingqidediyitian

  let v_date = substr(v_BeginTime,1,4)||'-'||substr(v_BeginTime,5,2)

  ||'-'||substr(v_BeginTime,7,2)||' 00:00:00';

  let v_week = weekday(v_date);

  let v_date = v_date - v_week UNITS day;

   Oracle

  -- zhijiezhuanhuanchengriqi

  v_BeginDate := to_date(v_BeginTime, 'yyyymmddhh24miss');

  -- zhegeyuedediyitian

  v_BeginDate := trunc(to_date(v_BeginTime, 'yyyymmddhh24miss'), mm);

  -- zhegexingqidediyitian

  v_BeginDate := trunc(to_date(v_BeginTime, 'yyyymmddhh24miss'), day);

  5shiwu

  zaioraclezhongqueshengqingkuangxiayigeshiwudejieshujiushixiayigeshiwudekaishisuoyiduiyuyigeshiwulaishuowomenzhiyaoxiecommitjikebuxuyaomingquebiaochushenmeshihoukaishiyigeshiwuerinformixxuyao

  6dayintiaoshixinxi

  7Informix

  --shezhigenzongmoshi

  set debug file to "trace_check"; -- with append;

  --shuomingwith appendbiaoshiyizhuijiamoshidakaigenzongjieguowenjian

  trace 'kaishizhixingcunchuguocheng'

  trace 'v_date='||v_date;

  trace cunchuguochengzhixingwanbi

  trace off;

  zhixingwanyihoudakaidangqianmuluxiadetrace_checkjikekandaodayinchulaidexinxi

  8Oracle

  DBMS_OUTPUT.PUT_LINE(kaishizhixingcunchuguocheng);

  DBMS_OUTPUT.PUT_LINE('v_date='||v_date);

  DBMS_OUTPUT.PUT_LINE(cunchuguochengzhixingwanbi);

  xianshezhiyixiahuanchongqudedaxiao

  set serveroutput on size 100000; -- ruguobuzhixinggaiyujuhuikanbudaotiaoshixinxi

  zhixingwanbiyihoudayinchulaidexinxijiuhuizhijiexianshizaijiemianshang

  5guanyushenshudeshuoming

  ruguocunchuguochengxiangfanhuiyigeshenshuzaiinformixzhongshitongguofanhuizhidexingshishixiandeerzaioracleshitongguoshuchushenshuhuozheshurushuchushenshushixiande

  juli

  1Informix:

  create procedure p1() returning integer;

  return 0;

  end procedure;

  2oracle:

  create or replace procedure p1(x out number)

  as

  begin

  x := 0;

  end p1;

  6fuzhi

  1informix

  let v_1 = 100;

  2oracle

  v_1 := 100;

  7ifyuju

  1informix

  if (v_1 =100) then

  elif (v_1=200) then

  Else

  end if;

  2oracle

  if (v_1 =100) then

  elsif (v_1=200) then

  Else

  end if;原文
 
 
在工作过程中涉及到过有时要进行INFORMIX和ORACLE之间的移植,有时应用需要既支持INFORMIX数据库又要支持ORACLE数据库,如果应用程序涉及到存储过程的话,就需要既熟悉INFORMIX存储过程的写法,又需要熟悉ORACLE存储过程的写法。笔者将在工作中总结的一些经验写出来与大家共享。 建立存储过程的语法: 一、Informix create procedure proc_name( [....in_parameter_list]) returning out_para_list / out_result_set; 二、Oracle create [or replace] procedure procedue_name [ (arg1 [ {in | out | in out }] type (argn [ {in | out | in out }] type,)] {is | as} --代替DECLARE关键字 [ 变量定义区] begin end procedure_name; 三、几个简单的例子 1、没有参数也没有返回值 1)Informix create procedure pNoParam() begin on exception rollback work; return; end exception begin work; delete from t1; delete from t2; commit work; end; end procedure; 2)Oracle create or replace procedure pNoParam as begin delete from t1; delete from t2; commit; exception when others then begin rollback; end; end pNoParam; 2、有输入输出 往t1表中插入一条记录,返回值表示插入是否成功。 1)Informix create procedure pNormalParam(f1 integer, f2 varchar(10)) returning integer; begin on exception rollback work; return -1; end exception begin work; insert into t1 values(f1, f2); commit work; return 0; 2)Oracle create or replace procedure pNormalParam(f1 number, f2 varchar2, v_Result out number) as begin insert into t1 values(f1,f2); commit; v_Result = 0; return; exception when others then begin rollback; v_Result := -1; end; end pNormalParam; 需要注意的是,在oracle存储过程中,参数是不能加上size的,比如f1,在t1表中该字段是number(10,0),而这里只能写number,而不能写number(10,0)。 3、返回记录集 1)Informix create procedure pReturnSet() returning integer, varchar(10); define i integer; define j varchar(10); foreach select f1, f2 into i, j from t1 return i, j with resume; end foreach; end procedure; 2)Oracle create or replace package TestRefCursorPkg as type TestRefCursorTyp is ref cursor; procedure pReturnSet(RefCursor out TestRefCursorTyp); end TestRefCursorPkg; create or replace package body TestRefCursorPkg as procedure pReturnSet (RefCursor out TestRefCursorTyp) as localCursor TestRefCursorTyp; begin open localCursor for select f1, f2 from t1; RefCursor := localCursor; end pReturnSet; end TestRefCursorPkg; / 四、其他差异说明 1、错误捕捉 1)Informix 使用 on exception end exception 2)Oracle 使用 exception when others then 2、对游标的处理 1)Informix create procedure pHasCursor() define v_f1 integer; begin on exception rollback work; return; end exception begin work; foreach curt1 with hold for select f1 into v_f1 from t1 -- 注意这里没有分号 if (v_f1 = 1) then update t1 set f2 = 'one' where current of curt1; elif (v_f1 = 2) then update t1 set f2 = 'two' where current of curt1; else update t1 set f2 = 'others' where current of curt1; end if; end foreach; commit work; end; end procedure; 2)Oracle create or replace procedure pHasCursor as v_f1 number(10,0); cursor curt1 is select f1 from t1 for update; begin open curt1; loop fetch curt1 into v_f1; exit when curt1%notfound; if (v_f1 = 1) then update t1 set f2 = 'one' where current of curt1; elsif (v_f1 = 2) then update t1 set f2 = 'two' where current of curt1; else update t1 set f2 = 'others' where current of curt1; end if; end loop; commit; return; exception when others then begin rollback; end; end pHasCursor; 3、在存储过程中调用另外一个存储过程 1)Informix Call pNoParam(); Call pNormalParam(1, ‘a’) returning v_Result; 2)Oracle pNoParam; pNormalParam(1, ‘a’, v_Result); 4、日期操作 1)当前时间 ① Informix define cur_dtime_var datetime year to second; 当前日期时间: let cur_dtime_var = current; -- datetime ② Oracle Currtime date; Currtime := sysdate; 2)当前日期的增减 ① Informix let tmp_date = today + 3 UNITS day; -- 当前时间加三天 let tmp_datetime = current + 1 UNITS second; -- 当前时间加1秒种 ② Oracle Tmp_date := sysdate + 3; -- 当前时间加三天 Tmp_date := sysdate + 1/24/3600; --当前时间加1秒种 3)日期转换成字符串 ① Informix let v_PeriodEndTime = year(v_date)||extend(v_date,month to month) ||extend(v_date,day to day) ||extend(v_date,hour to hour) ||extend(v_date,minute to minute)|| extend(v_date,second to second); ② Oracle v_PeriodEndTime := to_char(v_date, 'yyyymmddhh24miss'); 4)字符串转换成日期 假设字符串的形式是yyyymmddhhmiss形式的 ① Informix -- 直接转换成日期 let v_BeginDate = substr(v_BeginTime,1,4)||'-'||substr(v_BeginTime,5,2) ||'-'||substr(v_BeginTime,7,2)||' '||substr(v_BeginTime,9,2) ||':'||substr(v_BeginTime,11,2)||':'||substr(v_BeginTime,13,2); -- 这个月的第一天 let v_date = substr(v_BeginTime,1,4)||'-' ||substr(v_BeginTime,5,2)||'-1 00:00:00'; -- 这个星期的第一天 let v_date = substr(v_BeginTime,1,4)||'-'||substr(v_BeginTime,5,2) ||'-'||substr(v_BeginTime,7,2)||' 00:00:00'; let v_week = weekday(v_date); let v_date = v_date - v_week UNITS day; ② Oracle -- 直接转换成日期 v_BeginDate := to_date(v_BeginTime, 'yyyymmddhh24miss'); -- 这个月的第一天 v_BeginDate := trunc(to_date(v_BeginTime, 'yyyymmddhh24miss'), ‘mm’); -- 这个星期的第一天 v_BeginDate := trunc(to_date(v_BeginTime, 'yyyymmddhh24miss'), ‘day’); 5)事务 在oracle中缺省情况下,一个事务的结束就是下一个事务的开始,所以对于一个事务来说,我们只要写commit;即可,不需要明确标出什么时候开始一个事务,而informix需要。 6)打印调试信息 7)Informix --设置跟踪模式 set debug file to "trace_check"; -- with append; --说明“with append”表示以追加模式打开跟踪结果文件 trace '开始执行存储过程' trace 'v_date='||v_date; trace ‘存储过程执行完毕’ trace off; 执行完以后打开当前目录下的trace_check即可看到打印出来的信息。 8)Oracle DBMS_OUTPUT.PUT_LINE(‘开始执行存储过程’); DBMS_OUTPUT.PUT_LINE('v_date='||v_date); DBMS_OUTPUT.PUT_LINE(‘存储过程执行完毕’); 先设置一下缓冲区的大小 set serveroutput on size 100000; -- 如果不执行该语句,会看不到调试信息 执行完毕以后,打印出来的信息就会直接显示在界面上。 5、关于参数的说明 如果存储过程想返回一个参数,在informix中是通过返回值的形式实现的,而在oracle是通过输出参数或者输入输出参数实现的。 举例: 1)Informix: create procedure p1() returning integer; return 0; end procedure; 2)oracle: create or replace procedure p1(x out number) as begin x := 0; end p1; 6、赋值 1)informix let v_1 = 100; 2)oracle v_1 := 100; 7、if语句 1)informix if (v_1 =100) then elif (v_1=200) then Else end if; 2)oracle if (v_1 =100) then elsif (v_1=200) then Else end if;
󰈣󰈤
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

网络安全治理:国家安全保障的主要方向是打击犯罪,而不是处置和惩罚受害者

来源:中国青年报 新的攻击方法不断涌现,黑客几乎永远占据网络攻击的上风,我们不可能通过技术手段杜绝网络攻击。国家安全保障的主要方向是打击犯罪,而不是处置和惩罚...

 
 
 
>>返回首页<<
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 
 热帖排行
 
黑色魅力_醉人的美
梦幻蒙眬_如痴如醉
北京的“尖叫”建筑
高天上流云
 
 
王朝网络微信公众号
微信扫码关注本站公众号wangchaonetcn
 
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
©2005- 王朝网络 版权所有