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;

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值