oracle 一步一步学习存储过程

 

1.存储过程入门学习:

 

--创建一个简单的存储过程

create or replace procedure testPro 

is

begin 

Dbms_Output.put_line('hello word ! this is first produce');

end;

 

2.--创建一个带输入,输出参数的存储过程

create or replace procedure testProInOrOut

(

       a in number,

       z out number

)

is 

begin

--将a的值赋给 z 

 z  := a;

 Dbms_Output.put_line('输入值 '|| a || '  输出值   : '||z);

end ;

 

--测试带有输出参数的存储过程

-- Created on 2011-1-19 by ADMINISTRATOR 

declare 

  -- Local variables here

  i number;

begin

  -- Test statements here

  testProInOrOut(32,i);

end;

 

3.

--3.创建一个带有逻辑判断的存储过程

create or replace procedure testJudgePro

(

       a in number,

       z out varchar2

)

is 

begin

       Dbms_Output.put_line('当前你的等级');

       if a>=90 then 

       begin z:= 'A';

       end;

       end if;

       if a<90 then 

       begin z:='B';

       end;

       end if;

       if a<80 then 

       begin z:='C';

       end;

       end if;

       if a<70 then 

       begin z:='D';

       end;

       end if;

       if a<60 then 

       begin z:='E';

       end;

       end if;

      Dbms_Output.put_line('当前你的等级是:'||z);

       

end;

--存储过程测试:

declare 

  -- Local variables here

  i varchar2(2);

begin

  -- Test statements here

  testJudgePro(90,i);

end;

 

4.--建立一个带循环逻辑的存储过程:近似累加函数 

create or replace procedure testSum

(

      a in number,

      z out varchar2

)

is

tempResult number(16);

begin

     tempResult:=0;

      Dbms_Output.put_line('当前临时变量 a '||a);

     for tempa in 0..a loop

        begin 

          tempResult :=tempResult+tempa;

          Dbms_Output.put_line('当前临时变量 '||tempResult);

        end;

     end loop;

     

    z := tempResult; 

    Dbms_Output.put_line('累加结果为 '||z); 

end;

--测试存数过程

declare 

  -- Local variables here

  i varchar2(16);

begin

  -- Test statements here

  testSum(10,i);

end;

 

5.

--建立一个能从数据库中特定表中返回数据的存储过程

create or replace procedure testRebackDb

(

       z out varchar

 )

 is 

 tempNum number(16);

 begin

      tempNum:='start--->';

      select count(*) into tempNum from nrmws_region;

      

      z := tempNum;

      Dbms_Output.put_line('系统中一共有'||z||' 个地市!');

       

 end;

--测试

declare 

  -- Local variables here

  i varchar2(16);

begin

  -- Test statements here

  testRebackDb(i);

end;

 

6.

--建立一个能使用游标的带循环的存储过程:

create or replace procedure testCursor

(

       z out varchar2

)

is

tempRe varchar2(1024);

cursor tempcursor is  select * from nrmws_region nr where nr.parentid= '001';

begin

       tempRe:='start-->';

       for cursorRe in tempcursor loop

           begin

             tempRe :=   tempRe || cursorRe.name || '   ';

             Dbms_Output.put_line('当前地市 :'||cursorRe.name);

            end;

       end loop;

       z :=tempRe;

       Dbms_Output.put_line('结果集 :'||z);

end;

--测试

declare 

  -- Local variables here

  i varchar2(1024);

begin

  -- Test statements here

  testCursor(i);

end;

你可能感兴趣的:(oracle)