oracle编写存储过程的注意事项

Oracle 存储过程的基本语法

  1.基本结构 
    CREATE OR REPLACE PROCEDURE 存储过程名字
    (
        参数1 IN NUMBER,
        参数2 IN NUMBER
    ) IS
    变量1 INTEGER :=0;
    变量2 DATE;
    BEGIN

    END 存储过程名字;

  2.SELECT INTO STATEMENT
     将select查询的结果存入到变量中,可以同时将多个列存储多个变量中,必须有一条
      记录,否则抛出异常(如果没有记录抛出NO_DATA_FOUND)
      例子: 
        BEGIN
        SELECT col1,col2 into 变量1,变量2 FROM typestruct where xxx;
        EXCEPTION
        WHEN NO_DATA_FOUND THEN
            xxxx;
        END;

  例:

复制代码
CREATE OR REPLACE PROCEDURE myprowithout(snum IN NUMBER,
                                         s_name OUT VARCHAR2,
                                         s_sex OUT VARCHAR2)
AS
BEGIN
  SELECT s.sname, s.ssex INTO s_name, s_sex FROM student s WHERE s.sno=snum;
END;
复制代码

 

                                                   关于oracle存储过程的若干问题

  1.在oracle中,数据表别名不能加as,如:

      select a.appname from appinfo a;-- 正确
      select a.appname from appinfo as a;-- 错误
       也许,是怕和oracle中的存储过程中的关键字as冲突的问题吧

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

      select af.keynode into kn from APPFOUNDATION af where af.appid=aid and af.foundationid=fid;-- 有into,正确编译
      select af.keynode from APPFOUNDATION af where af.appid=aid and af.foundationid=fid;-- 没有into,编译报错,提示:Compilation 
      Error: PLS-00428: an INTO clause is expected in this SELECT statement
  3.在利用select...into...语法时,必须先确保数据库中有该条记录,否则会报出"no data found"异常。
       可以在该语法之前,先利用select count(*) from 查看数据库中是否存在该记录,如果存在,再利用select...into...
  4.在存储过程中,别名不能和字段名称相同,否则虽然编译可以通过,但在运行阶段会报错
     select keynode into kn from APPFOUNDATION where appid=aid and foundationid=fid;-- 正确运行
     select af.keynode into kn from APPFOUNDATION af where af.appid=appid and af.foundationid=foundationid;-- 运行阶段报错,提示
     ORA-01422:exact fetch returns more than requested number of rows
  5.在存储过程中,关于出现null的问题
    假设有一个表A,定义如下:
    create table A(
      id varchar2(50) primary key not null,
     vcount number(8) not null,
      bid varchar2(50) not null -- 外键 
    );
    如果在存储过程中,使用如下语句:
    select sum(vcount) into fcount from A where bid='xxxxxx';
     如果A表中不存在bid="xxxxxx"的记录,则fcount=null(即使fcount定义时设置了默认值,如:fcount number(8):=0依然无效,
     fcount还是会变成null),这样以后使用fcount时就可能有问题,所以在这里最好先判断一下:
      if fcount is null then
          fcount:=0;
        end if;
这样就一切ok了
 

oracle存储过程—-赋值、相等、分支的写法
  忽然想起竟然没有记录下存储过程中关于赋值 、相等 、分支 的写法。

赋值
  在存储过程 中 赋值的写法是:= 比如i := 0 ,表示将0 赋值给i 的意思。

相等/不等
  在存储过程 中 相等的写法是= 比如i = 0 ,表示判断i是否等于0的意思,相等则为true ,反之,则为false;当然也有不等,即<> 。

分支
  在存储过程 中 分支的写法,与我们平时的常用语言基本一样:
平时,基本都是:if(条件1) ...else if(条件2) ... else
存储过程中变成是if(条件1)then ...elsif(条件2)then .... else...end if

具体的例子:
  因为比较简单,我把它们三个放到了一起,写在下边了:

create or replace procedure test_select5_procedure
(innum  number,outnum out number)
AS
BEGIN
   if (innum > 5) then --(判断条件是否大于5 ,满足条件则以then标识进入当前分支)
       if(innum = 10) then -- (= 为相等)
          outnum := 20;   -- (:= 表示赋值)
       else
         outnum := innum + 1 ;
       end if;            --- (每个分支的结束都以 end if;来结束)
      dbms_output.put_line('第一个分支:'||outnum);
    elsif (innum > 0) then  --- (elsif 的写法注意,亲测必须这么写才行,写成else if不行的)
      outnum := innum - 1 ;
      dbms_output.put_line('第二个分支:'||outnum);
    else
      outnum := -1 ;
      dbms_output.put_line('第三个分支:'||outnum);
    end if;  --- (每个分支的结束都以 end if;来结束)
END;

执行存过语句

declare  
i number(10);
begin
test_select5_procedure(6,i);end;

结果为:

第一个分支:7

create or replace procedure test_procedure(a in number,b out varchar)
AS
begin
b:='33'; --这是赋值语句
dbms_output.put_line('第一个存储过程a的值:' || a); --输出值的时候要加||
end;
————————————————
DECLARE i varchar(10);

--这里错误写成了varchar,忘了加长度

begin test_procedure(1,i);

dbms_output.put_line(i);

end;

在调用传出参数的时候,发现必须要定义变量,即DECLARE i varchar(10) ,定义完变量后再传到存储过程 中,执行完存储过程后,这个i 上就会有执行后的值,接着再输出,即可看到结果。
  上边这个把我给坑了,当时不管怎么执行都报错,后来找了个大神看了下,原来我上边i varchar(10) ,错误写成了i varchar ,没有定义长度,看来,存储过程在括号里边定义的时候,不需要加长度,但是在定义变量的时候,必须要有长度。

原文链接:https://blog.csdn.net/wohaqiyi/article/details/81660229

原文地址:https://www.cnblogs.com/thomasbc/p/15648660.html