Oracle存储过程Procedure简单介绍(第三部分)

我们在上面章节里面介绍了存储过程的绝大部分内容,但是没有介绍存储过程的核心:控制和循环语句
1、控制结构
if-then
if-then-else
if-then-elsif-else
主要就这三种控制结构
下面举个例子说明一下:
create or replace procedure mypro(spNo number) is
  v_job emp.job%type;
begin
  select job into v_job from emp where empno = spNo;
  if v_job = 'PRESIDENT' then
    update emp set sal = sal + 1000 where empno = spNo;
  elsif v_job = 'MANAGER' then
    update emp set sal = sal + 500 where empno = spNo;
  else
    update emp set sal = sal + 200 where empno = spNo;
  end if;
end;
/
执行
begin
  -- Call the procedure
  mypro(49);
end;


2、循环语句
pl/sql主要的循环语句就下面这五种:
loop-end loop
exit when
while
for(普通循环)
for(游标循环)
下面逐一进行举例:


a、loop循环
create or replace procedure proc_loop is
i number;
begin
  i := 0;
  loop
    i:=i+1;
    dbms_output.put_line(i);
    if i > 5 then
      exit;
    end if;
  end loop;
end;


b、exit when循环
create or replace procedure proc_exit is
i number;
begin
  i := 0;
  loop
    exit when(i>5);
    dbms_output.put_line(i);
    i := i+1;
  end loop;
end;


c、while循环
create or replace procedure proc_while is
i number;
begin
  i := 0;
  while i<5 loop
    i:=i+1;
    dbms_output.put_line(i);
  end loop;
end;


d、for普通循环
create or replace procedure proc_for is
  i number;
begin
  i := 0;
  for i in 1 .. 5 loop
    dbms_output.put_line(i);
  end loop;
end;


e、for游标循环
create or replace procedure proc_for_cursor is
  userRow emp%Rowtype;
  cursor userRows is
    select * from emp;
begin
  for userRow in userRows loop
    dbms_output.put_line(userRow.empno || ' , ' || userRow.ename || ' , ');
  end loop;
end;

这章的内容粗略的给大家介绍了pl/sql的控制和循环语句的用法。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31358702/viewspace-2154095/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/31358702/viewspace-2154095/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值