pl/sql之控制结构(分支,循环,控制)

pl/sql的进阶--控制结构
 介绍
  在任何计算机语言(c,java,pascal)都有各种控制语句(条件语句,循环结构,顺序控制结构...)在pl/sql中也存在这样的控制结构。
在本部分学习完成后,希望大家达到:
1.使用各种if语句
2.使用循环语句
3.使用控制语句——goto和null;

条件分支语句
pl/sql中提供了三种条件分支语句if—then,if – then – else,if – then – elsif – then
这里我们可以和java语句进行一个比较

 简单的条件判断 if – then
问题:编写一个过程,可以输入一个雇员名,如果该雇员的工资低于2000,就给该员工工资增加10%。

Sql代码:
  1. create or replace procedure sp_pro6(spName varchar2) is  
  2. --定义   
  3. v_sal emp.sal%type;   
  4. begin  
  5.     --执行   
  6.     select sal into v_sal from emp where ename=spName;   
  7.     --判断   
  8.     if v_sal<2000 then  
  9.         update emp set sal=sal+sal*10% where ename=spName;   
  10.     end if;   //结束这个分支
  11. end;   
  12. /  

 二重条件分支 if – then – else
问题:编写一个过程,可以输入一个雇员名,如果该雇员的补助不是0就在原来的基础上增加100;如果补助为0就把补助设为200;

Sql代码:
  1. create or replace procedure sp_pro6(spName varchar2) is  
  2. --定义   
  3. v_comm emp.comm%type;   
  4. begin  
  5.     --执行   
  6.     select comm into v_comm from emp where ename=spName;   
  7.     --判断   
  8.     if v_comm<>0 then  
  9.         update emp set comm=comm+100 where ename=spName;   
  10.     else  
  11.         update emp set comm=comm+200 where ename=spName;   
  12.     end if;   
  13. end;   
  14. /  

多重条件分支 if – then – elsif – then
问题:编写一个过程,可以输入一个雇员编号,如果该雇员的职位是PRESIDENT就给他的工资增加1000,如果该雇员的职位是MANAGER就给他的工资增加500,其它职位的雇员工资增加200。

Sql代码:
  1. create or replace procedure sp_pro6(spNo number) is  
  2.     --定义   
  3.     v_job emp.job%type;   
  4. begin  
  5.     --执行   
  6.     select job into v_job from emp where empno=spNo;   
  7.     if v_job='PRESIDENT' then  
  8.         update emp set sal=sal+1000 where empno=spNo;   
  9.     elsif v_job='MANAGER' then  //注意这里要用单引号啊,不要用JAVA里的双引号
  10.         update emp set sal=sal+500 where empno=spNo;   
  11.     else  
  12.         update emp set sal=sal+200 where empno=spNo;   
  13.     end if;   
  14. end;   
  15. /  

循环语句 –loop
  是pl/sql中最简单的循环语句,这种循环语句以loop开头,以end loop结尾,这种循环至少会被执行一次。这个有点像JAVA里面的do..while
案例:现有一张表users,表结构如下:
用户id | 用户名
       |
请编写一个过程,可以输入用户名,并循环添加10个用户到users表中,用户编号从1开始增加。

Sql代码:
  1. create or replace procedure sp_pro6(spName varchar2) is  
  2. --定义  :=表示赋值   
  3.     v_num number:=1;       
  4. begin  
  5.     loop   
  6.         insert into users values(v_num,spName);   
  7.         --判断是否要退出循环   
  8.         exit when v_num=10;   
  9.         --自增   
  10.         v_num:=v_num+1;   
  11.     end loop;   
  12. end;   
  13. /  

循环语句 –while循环
  基本循环至少要执行循环体一次,而对于while循环来说,只有条件为true时,才会执行循环体语句,while循环以while...loop开始,以end loop结束。
案例:现有一张表users,表结构如下:
用户id 用户名

问题:请编写一个过程,可以输入用户名,并循环添加10个用户到users表中,用户编号从11开始增加。

Sql代码:
  1. create or replace procedure sp_pro6(spName varchar2) is  
  2. --定义  :=表示赋值   
  3.     v_num number:=11;       
  4. begin  
  5.     while v_num<=20 loop   
  6.     --执行   
  7.        insert into users values(v_num,spName);   
  8.        v_num:=v_num+1;   
  9.     end loop;   
  10. end;   
  11. /  

循环语句 –for循环 (不太建议使用)
基本for循环的基本结构如下

Sql代码:
  1. begin  
  2.   for i in reverse 1..10 loop   
  3.     insert into users values (i, 'shunping');   
  4.   end loop;   
  5. end;  
begin
  for i in reverse 1..10 loop
    insert into users values (i, 'shunping');
  end loop;
end;

我们可以看到控制变量i,在隐含中就在不停地增加。

 

顺序控制语句 –goto,null
1.goto语句
  goto语句用于跳转到特定符号去执行语句。注意由于使用goto语句会增加程序的复杂性,并使得应用程序可读性变差,所以在做一般应用开发时,建议大家不要使用goto语句。 在很多层for循环的时候可能会比较有用,用goto直接跳出循环
基本语法如下 goto lable,其中lable是已经定义好的标号名,

Sql代码:
  1. declare  
  2.   i int := 1;   
  3. begin  
  4.   loop   
  5.     dbms_output.put_line('输出i=' || i);   
  6.     if i = 1{} then  
  7.       goto end_loop;   
  8.     end if;   
  9.     i := i + 1;   
  10.   end loop;   
  11.   <<end_loop>>   
  12.   dbms_output.put_line('循环结束');   
  13. end;  

2.null
null语句不会执行任何操作,并且会直接将控制传递到下一条语句。使用null语句的主要好处是可以提高pl/sql的可读性,没有实际的作用

Sql代码:
  1. declare  
  2.   v_sal   emp.sal%type;   
  3.   v_ename emp.ename%type;   
  4. begin  
  5.   select ename, sal into v_ename, v_sal from emp where empno = &no;   
  6.   if v_sal < 3000 then  
  7.     update emp set comm = sal * 0.1 where ename = v_ename;   
  8.   else  
  9.     null;   
  10.   end if;   
  11. end;  

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yjsuge

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值