标题 | oracle代码的常见错误汇总 |
内容 | 很多使用其他数据库的朋友在转到Oracle数据库的时候经常会犯一些习惯性的错误,本文汇总了一些常见的Oracle代码错误,供大家查询比对。 1. 插入语句写成: view sourceprint?1 insert into t1 values(...) 应为: view sourceprint?1 insert into t1(...) values(...) 2. view sourceprint?1 to_char(sysdate,'YYYYMMDDHHMMSS') 应改为: view sourceprint?1 to_char(sysdate,'YYYYMMDDHH24MISS') 3. view sourceprint?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. view sourceprint?1 select c1,c2 in from t1 where c3 = v_c3; 2 if v_c1 = 0 or v_c2 = 0 then 执行该代码找不到时会报错,不会返回0 应该用when no_data_found判断 6. view sourceprint?1 '... where c1 = ' || v_c1; c1字段为字符型,要加单引号,否则用不上c1索引,oracle内部转换成了'... where c1 = ' ||chr(39)|| to_char(v_c1)||chr(39); 改为: view sourceprint?1 where c1 = '||chr(39) ||v_c1||chr(39); 7.如果只判断是否存在数据,应加and rownum<2速度更快 view sourceprint?1 select count(*) 2 into v_count 3 from t1 4 where c1 = v_c1; 应加条件:and rownum<2 8. view sourceprint?1 WHERE EXISTS (SELECT * 2 FROM t1 应为: view sourceprint?1 WHERE EXISTS (SELECT 'x' 2 FROM t1 9. view sourceprint?1 RAISE_APPLICATION_ERROR(-20000, infoMsg); 不要都用20000错误号 |
随便看 |
|
在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。