服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Oracle - oracle代码的常见错误汇总

oracle代码的常见错误汇总

2019-12-10 15:20shichen2014 Oracle

这篇文章主要介绍了oracle代码的常见错误,需要的朋友可以参考下

很多使用其他数据库的朋友在转到Oracle数据库的时候经常会犯一些习惯性的错误,本文汇总了一些常见的Oracle代码错误,供大家查询比对。

1. 插入语句写成:

?
1
insert into t1 values(...)

应为:

?
1
insert into t1(...) values(...)

2.

?
1
to_char(sysdate,'YYYYMMDDHHMMSS')

应改为:

?
1
to_char(sysdate,'YYYYMMDDHH24MISS')

3.

?
1
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.

?
1
2
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. 

?
1
'... where c1 = ' || v_c1;

c1字段为字符型,要加单引号,否则用不上c1索引,oracle内部转换成了'... where c1 = ' ||chr(39)|| to_char(v_c1)||chr(39);
改为:

?
1
where c1 = '||chr(39) ||v_c1||chr(39);

7.如果只判断是否存在数据,应加and rownum<2速度更快

?
1
2
3
4
select count(*)
into v_count
from t1
where c1 = v_c1;

应加条件:and rownum<2

8.

?
1
2
WHERE EXISTS (SELECT *
FROM t1

应为:

?
1
2
WHERE EXISTS (SELECT 'x'
FROM t1

9.

?
1
RAISE_APPLICATION_ERROR(-20000, infoMsg);

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

延伸 · 阅读

精彩推荐