oracle的存储过程语法

1、ORA-00942: table or view does not exist 指的你要操作的表尚未存在,需要先create出来先。

2、ORA-00922: missing or invalid option 指的是有语法错误。遗漏了分号什么的

3、Warning: Procedure created with compilation errors

比如  create or replace procedure p_test_pro1 is
    begin
      insert into loginuser(username,passwd) values('admin','123');
    end                -------这里遗漏了分号
    /
 
Warning: Procedure created with compilation errors
 
SQL> show error  --显示出具体的错误所在
Errors for PROCEDURE SCOTT.P_TEST_PRO1:
 
LINE/COL ERROR
-------- ----------------------------------------------------------------------------------------------------
6/0      PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:       ; <an identifier> <a double-quoted delimited-identifier>     delete exists prior <a single-quoted SQL string>  The symbol ";" was substituted for "end-of-file" to continue. 


4、ORA-06576: not a valid function or procedure name     存储过程p_test_pro1尚未创建

call p_test_pro1
 
ORA-06576: not a valid function or procedure name

 

5、ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at "SCOTT.P_TEST_PRO1", line 6
ORA-06512: at line 2

 

create or replace procedure p_test_pro1 IS
               v_pass varchar2(20);
           begin
               select passwd into v_pass from loginuser where rownum=1;
               dbms_output.put_line('密码是:'+v_pass);
            end;

当调用p_test_pro1的时候报以上错误,把输出语句中的+改为||

 

 在谈存储过程书写中的一些规则时,先看一下执行它的规则,在命令窗口执行存储过程sp_get_product_prompt
     set serveroutput on
     var ret1 varchar2(200);
     var ret2 varchar2(200);
     exec sp_get_product_prompt(83,:ret1,:ret2); --或execute
     print ret1;
     print ret2;
    
     set serveroutput on
     declare
        ret1 varchar2(200);
        ret2 varchar2(200);
     begin
        sp_get_product_prompt(83,ret1,ret2);
        dbms_output.put_line(ret1);
        dbms_output.put_line(ret2);
     end;

存储过程入参,不论类型,缺省情况下值都为null,入参和出参不能有长度,其中关键字as可以替换成is,存储过程中变量声明在as和begin之间,同时,存储过程中可以再调用其它的存储过程,如果要保证存储过程之间的事务处理不受影响,可以定义为自治事务。
     create or replace procedure say_hello(
       v_name in varchar2,
       v_flag number,
       o_ret out number
     )
     as
     begin
       if v_name is null and v_flag is null then
--v_name和v_flag都等于null
           o_ret := 10;
       else
           o_ret := 100;
       end if;
     end;

对于入参为null情况下给予缺省值
     create or replace procedure say_hello(
       i_name in varchar2,
       i_flag number,
       o_ret out number
     )
     as
       v_name  varchar2(100);
     begin
       if i_name is null then
          v_name := '0';
       else
          v_name := i_name;
       end if;
       insert into phone(..,wname..,) values(..,v_name,..); 
     
     end;
或直接在insert语句中调用nvl函数赋缺省值
     insert into phone(..,wname..,) values(..,nvl(v_name,' '),..); ----如果将' '写成'',则insert进来的v_name值还是为''等价于null值

带一个参数的存储过程
   输入参数in,输入参数不能进行:=赋值,但可以将它赋给as后面定义的变量;
   输入参数in,可以作为变量进行条件判断;
   默认不写就是in;
   存储过程没有重载,这个有参的say_hello会替代已经存在的无参say_hello。

     create or replace procedure say_hello(v_name in varchar2)
     as
     begin
        --v_name:='a';
--存储过程入参v_name不能做为赋值目标
        dbms_output.put_line('hello '||v_name); 
     end;

存储过程输入参数作为变量进行条件判断
     create or replace procedure say_hello(
        i_opFlag in number
     )
     as
        v_name varchar2(100);
     begin
        if i_opFlag = 1 then
    v_name :='0'; 
        else
    v_name :='haha';
        end if; 
        dbms_output.put_line('hello '||v_name); 
     end;


利用存储过程中定义的变量对入参的空值处理:
     create or replace procedure say_hello(
        i_name in varchar2
     )
     as
        v_name varchar2(100);
     begin
        if i_name is null then
    v_name :='0'; 
        else
    v_name :=i_name;--将入赋值给定义变量
        end if; 
        dbms_output.put_line('hello '||v_name); 
     end;

多个参数的存储过程
     create or replace procedure say_hello(
        v_first_name in varchar2,
        v_last_name in varchar2)
     as
     begin
        dbms_output.put_line('hello '||v_first_name||'.'||v_last_name);
     end;

out输出参数,用于利用存储过程给一个或多个变量赋值,类似于返回值
     create or replace procedure say_hello(
        v_name in varchar2,
        v_content out varchar2
     )
     begin
        v_content:='hello'||v_name;
     end;

     调用:
     declare
        v_con varchar2(200);
        v_in varchar2(20):='wang';
     begin
        say_hello(v_in,v_con);
        dbms_output.put_line(v_con);
     end;

in out参数,既赋值又取值
     create or replace procedure say_hello(v_name in out varchar2)
     as
     begin
        v_name:='hi '||v_name;
     end;

     调用:
     declare
        v_inout varchar2(20):='wangsu';
     begin
        say_hello(v_inout);
        dbms_output.put_line(v_inout);
     end;

对存储过程入参赋缺省值
     create or replace procedure say_hello(
        v_name varchar2 default 'susu',
        v_content varchar2 default 'hello'
     )
     as
     begin
        dbms_output.put_line(v_name||' '||v_content);
     end;

     调用:(用指明形参名的方式调用更好)
     begin
        say_hello();
     end;
     或
     begin
        say_hello('cheng');
     end;
     或
     begin
     say_hello(v_name=>'cheng');
    end;

你可能感兴趣的:(oracle,sql)