关于oracle存储过程需要注意的问题

  在使用oracle存储过程时,有一些需要注意的地方,下面就来总结一下。

1.在oracle的存储过程中,数据表别名不能加as

  也许是为了区分存储过程中的as,怕与过程中的as冲突。

  如:

    select * from appinfo a;  --正确

    select * from appinfo as a;  --错误

2.在oracle的存储过程中,select某一字段时,后面必须紧跟into,如果select整个记录,利用游标的话就另当别论了

  如:

    select u.name into uname

    from user u

    --有into,编译正确。

    

    select u.name 

    from user u

    --没有into,编译报错,提示:Compilation   Error: PLS-00428: an INTO clause is expected in this SELECT statement.

3.在利用select...into...语法时,要必须先确保数据库有该条语句

  

  在使用select...into...语句前可以使用select count(*)from查看数据库中是否有数据存在,如果有数据再使用select...into...语句,否则会报出"no data found"

4.在存储过程中,别名不能和字段名称相同,否则虽然编译可以通过,但在运行时会报错

  会报出ORA-01422:exact fetch returns more than requested number of rows

5.在oracle存储过程中,关于出现null的问题。

  假设有一个user表,如下:

    create table user (

    id varchar2(50) primary key not null,

    u_name varchar2(50) not null,

    u_age varchar2(50) not null,

    );

  如果在存储过程中,使用如下的语句:

    select sum(u_age) into fcount from user where u_age = 'xxx';

  如果user表中不存在u_age='xxx'的记录,则fcount=null(即使fcount定义时设置了默认值,依然

  无效,fcount还是会变成null),这样的话以后使用fcount时有可能会出现问题,所以在这里最好先判

  断一下:

  if fcount is null then

    fcount := 0;

  end if;

  这样就可以解决了。

  

  

原文地址:https://www.cnblogs.com/wuyx/p/6441747.html