一、条件分支语句
1、if判断
if <布尔表达式> then pl/sql 和 sql语句 end if;
2、if else判断
if <布尔表达式> then pl/sql 和 sql语句 else 其它语句 end if;
3、if elsif else判断
if <布尔表达式> then pl/sql 和 sql语句 elsif < 其它布尔表达式> then 其它语句 elsif < 其它布尔表达式> then 其它语句 else 其它语句 end if;
例子:
declare v_first_name varchar2(20); v_hire_date date; v_bonus number(6,2); begin select first_name, hire_date into v_first_name, v_hire_date from employees where employee_id = &emp_id; if v_hire_date > to_date('01-1月-90') then v_bonus := 800; elsif v_hire_date > to_date('01-1月-88') then v_bonus := 1600; else v_bonus := 2400; end if; dbms_output.put_line(v_first_name||'雇员的雇佣日期是'||v_hire_date ||'、奖金是'||v_bonus); end;
4、case 表达式
语句段以分号结尾。
格式一:使用单一选择符,进行等值比较
case when 条件表达式1 then 语句段1 when 条件表达式2 then 语句段2 ...... when 条件表达式n then 语句段n [else 语句段] end;
例子:
declare v_grade char(1) := upper('&p_grade'); v_appraisal varchar2(20); begin v_appraisal := case v_grade when 'a' then 'excellent' when 'b' then 'very good' when 'c' then 'good' else 'no such grade' end; dbms_output.put_line('grade:'||v_grade||' appraisal: '|| v_appraisal); end;
格式二 :使用多种条件进行比较。
case 条件表达式 when 条件表达式结果1 then 语句段1 when 条件表达式结果2 then 语句段2 ...... when 条件表达式结果n then 语句段n [else 条件表达式结果] end;
例:
declare v_first_name employees.first_name%type; v_job_id employees.job_id%type; v_salary employees.salary%type; v_sal_raise number(3,2); begin select first_name, job_id, salary into v_first_name, v_job_id, v_salary from employees where employee_id = &emp_id; case when v_job_id = 'pu_clerk' then if v_salary < 3000 then v_sal_raise := .08; else v_sal_raise := .07; end if; when v_job_id = 'sh_clerk' then if v_salary < 4000 then v_sal_raise := .06; else v_sal_raise := .05; end if; when v_job_id = 'st_clerk' then if v_salary < 3500 then v_sal_raise := .04; else v_sal_raise := .03; end if; else dbms_output.put_line('该岗位不涨工资: '||v_job_id); end case; dbms_output.put_line(v_first_name||'的岗位是'||v_job_id ||'、的工资是'||v_salary ||'、工资涨幅是'||v_sal_raise); end;
二、循环语句
1、loop简单循环(至少执行一次)
loop 要执行的语句; exit when <条件语句> --条件满足,退出循环语句 end loop;
例:
declare int number(2) :=0; begin loop int := int + 1; dbms_output.put_line('int 的当前值为:'||int); exit when int =10; end loop; end;
2. while 循环
while <布尔表达式> loop 要执行的语句; end loop;
例:
declare x number :=1; begin while x<=10 loop dbms_output.put_line('x的当前值为:'||x); x:= x+1; end loop; end;
3、for数字式循环
可以使用exit 退出循环。注意不需要声明变量。加上reverse 表示反向循环。
[<<循环标签>>] for 循环计数器 in [ reverse ] 下限 .. 上限 loop 要执行的语句; end loop [循环标签];
例:
begin for int in 1..10 loop dbms_output.put_line('int 的当前值为: '||int); end loop; end;
三、标号和goto语句
嵌套循环和标号
declare result int:=0; begin <<outer>> for i in 1..20 loop <<inner>> for j in 1..10 loop result:=i*j; dbms_output.put_line('循环时,i='||i||',j='||j||',result的值:'||result); --当外层循环result值达到200时,跳出整个外层循环 exit outer when result = 200; --当内层循环result值达到100时,跳出内层循环 exit when result = 100; end loop inner; dbms_output.put_line('end inner ,result的值:'||result); end loop outer; dbms_output.put_line('end outter ,result的值:'||result); end;
goto子句:
goto label; ...... <<label>> /*标号是用<< >>括起来的标识符 */
declare v_counter number := 1; begin loop dbms_output.put_line('v_counter的当前值为:'||v_counter); v_counter := v_counter + 1; if v_counter > 10 then goto labeloffloop; end if; end loop; <<labeloffloop>> dbms_output.put_line('v_counter的当前值为:'||v_counter); end;
四、 null 语句
在pl/sql 程序中,null语句是一个可执行语句,可以用 null 语句来说明“不用做任何事情”的意思,相当于一个占位符或不执行任何操作的空语句,可以使某些语句变得有意义,提高程序的可读性,保证其他语句结构的完整性和正确性。如:
declare v_emp_id employees.employee_id%type; v_first_name employees.first_name%type; v_salary employees.salary%type; v_sal_raise number(3,2); begin v_emp_id := &emp_id; select first_name, salary into v_first_name, v_salary from employees where employee_id = v_emp_id; if v_salary <= 3000 then v_sal_raise := .10; dbms_output.put_line(v_first_name||'的工资是'||v_salary||'、工资涨幅是'||v_sal_raise); else null; end if; end;
到此这篇关于oracle数据库之pl/sql使用流程控制语句的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。
需要了解更多数据库技术:Oracle数据库之PL/SQL使用流程控制语句,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/dtteaching/1238229.html