oracle代碼常見錯誤

來源:互聯網
上載者:User

   1. insert into t1 values(...)

  應為:insert into t1(...) values(...)

  2.to_char(sysdate,'YYYYMMDDHHMMSS')

  改為:

  to_char(sysdate,'YYYYMMDDHH24MISS')

  3. select count(*) into v_count from t1 where c1=?;

  永遠不會觸發no_data_found錯誤,應該判斷v_count=0

  4.傳回值問題

  合理的處理辦法:

  在過程的開始 result:=false;

  在過程的結尾 result:=true;

  中間不需要對result賦值。

  不合理的處理辦法:

  在過程的開始 result:=true;

  中間代碼每一處判斷不符合時需result:=false賦值。

  在過程的結尾不需對result賦值

  5. select c1,c2 in from t1 where c3 = v_c3;

  if v_c1 = 0 or v_c2 = 0 then

  找不到時會報錯,不會返回0

  應該用when no_data_found判斷

  6. '... where c1 = ' || v_c1; c1欄位為字元型,要加單引號,否則用不上c1索引,oracle內部轉換成了'... where c1 = ' ||chr(39)|| to_char(v_c1)||chr(39);

  改為:where c1 = '||chr(39) ||v_c1||chr(39);

  7. 如果只判斷是否存在資料,應加and rownum<2速度更快

  select count(*)

  into v_count

  from t1

  where c1 = v_c1;

  應加條件:and rownum<2

  7. WHERE EXISTS (SELECT *

  FROM t1

  應為:

  WHERE EXISTS (SELECT 'x'

  FROM t1

  8. RAISE_APPLICATION_ERROR(-20000, infoMsg);不要都用20000錯誤號碼

  RAISE_APPLICATION_ERROR的可用錯誤號碼範圍為-20000至-20999

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.