【oracle代码常见错误】 oracle常见错误

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