oracle代码的常见错误汇总

很多使用其他数据库的朋友在转到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

8.

WHERE EXISTS (SELECT *

FROM t1

应为:

WHERE EXISTS (SELECT 'x'

FROM t1

9.

RAISE_APPLICATION_ERROR(-20000, infoMsg);

不要都用20000错误号

RAISE_APPLICATION_ERROR的可用错误号范围为-20000至-20999

以上是 oracle代码的常见错误汇总 的全部内容, 来源链接: utcz.com/z/352368.html

回到顶部