SqlServer儲存過程轉換成Oracle儲存過程語法常見問題
阿新 • • 發佈:2019-02-02
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必須存在