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(⑵0000, infoMsg);
不要都用20000毛病号
RAISE_APPLICATION_ERROR的可用毛病号范围为⑵0000至⑵0999
文章来源:丸子建站
文章标题:oracle代码的常见毛病汇总
https://www.wanzijz.com/view/41827.html