SqlServer存储过程转换成Oracle储存过程语法常见问题

1. top order by 转换成 rownum order by 的问题 (子查询实现)

同级情况下的优先处理顺序:
    sqlserver: 先order by 再top
    oracle:    先rownum 再 order by

2. 已有数据的字段类型不匹配,通过下列语句修改。

 alter table css_sed rename column action to myaction;

 alter table css_sed add (action varchar2(20));

 update css_sed set action = myaction;

 alter table css_sed drop column myaction;

3. 多个cast问题

 isnull(cast(cast(GR_WT as int) as varchar),'') sqlserver

 nvl(cast(trunc(cntr_row.GR_WT) as varchar2),'') plsql

4. if exists问题

   oracle 没有这个语法
   解决方法: 拿一个临时变量储存结果来判断
   begin
    select count(*) into V_CNT from all_tables where table_name = 'CUST_BC_ONLINE';
    if(V_CNT > 0)
    then
      execute immediate 'truncate table CUST_BC_ONLINE';
    end if; 
   end;

5. SqlServer: with index()语法问题

Oracle: 使用index hints实现  eg: select /*+ index(t i_t) */ * from t where username='EYGLE';

6. nvl(date,0) 问题

   isnull(MCS_SHPT_INFO.LAST_MOD_DT,0) < b.LAST_MOD_DT

   nvl(MCS_SHPT_INFO.LAST_MOD_DT,to_date('1900-01-01','yyyy-mm-dd'))) < b.LAST_MOD_DT

7. top 赋值问题

 select top(5) @param = column from table
 sqlserver默认会把最后一条数据的字段赋值给指定参数,oracle会报错,就是选择出来的行数不匹配。

8. declare的变量只能跟在后面的begin end 语句块使用

9. 字符串拼接问题

     oracle: null跟字符串拼接,会把null当成空字符串处理
     sqlserver: null跟字符串拼接,会把整个字符串设置为一个空格

10. Oracle异常信息打印

     dbms_output.put_line(dbms_utility.format_error_stack());
     dbms_output.put_line('----------------------------------------------------------------');
     dbms_output.put_line(dbms_utility.format_call_stack());
     dbms_output.put_line('----------------------------------------------------------------');
     dbms_output.put_line(dbms_utility.format_error_backtrace());
     dbms_output.put_line('----------------------------------------------------------------');
     dbms_output.put_line(sqlcode);
     dbms_output.put_line(sqlerrm);

11. SqlServer:select into 语法问题

     SqlServer:
         select into table2 from table1 
         备注:table2可以不存在
     Oracle:
         insert into table2 select * from table1 
         备注:要求table2必须存在
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章