PL_SQL基础--续五

存储过程

存储过程--起了名字的PL_SQL程序:
 create or replace procedure p
 is
  cursor c is
   selcet * from emp2 for update;
 begin
  for v_emp in c loop
   if(v_emp.deptno = 10) then
    update emp2 set sal = sal + 10 where current of c;
   elsif(v_emp.deptno = 20) then
    update emp2 set sal = sal + 20 where current of c;
   else
    update emp2 set sal =sal +50 where current of c;
   end if;
  end loop;
  commit;
 end;
 
 执行存储过程:
 1.exec p;
 2.begin
     p;
   end;

 --带参数的存储过程
 --in 传入参数,out 传出参数,不写默认为传入参数。一个参数可既为传入参数,又可为传出参数。
 create or replace procedure p
  (v_a in number, v_b number,v_ret out number,v_temp in out number)
 is

 begin
  if(v_a > v_b) then
    v_ret := v_a;
  else
    v_ret := v_b;
  end if;
  v_temp := v_temp + 1;
 end;

 --调用程序
 declare
  v_a number := 3;
  v_b number := 4;
  v_ret number;
  v_temp number := 5;
 begin
  p(v_a,v_b,v_ret,v_temp);
  dbms_output.put_line(v_ret);
  dbms_output.put_line(v_temp);
 end;

 需注意的是produre创建的过程中如出现语法错误,不会报错,只是出警告。
 需要show error命令把错误show出来。

 

用存储过程实现蚂蚁大战大象的树状结构的展现:
 create or replace procedure p(v_pid article.pid%type,v_level binary_integer)
 is
   cursor c is select * from article where pid = v_pid;
   v_preStr varchar2(1024) := '';
 begin
   for i in 1..v_level loop
     v_preStr := v_preStr || '***';
   end loop;
  
   for v_article in c loop
     dbms_output.put_line(v_preStr || v_article.cont);
     if(v_article.isleaf = 0) then
  p(v_article.id,v_level + 1);
     end if;
   end loop;
 end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值