使用IF语句按条件判断,控制PL/SQL执行流程:
if-then-end ifif-then-else-end ifif-then-elsif-then-else-end ifset serveroutput on
declare v_hire_date date := to_date('1995-01-15','yyyy-mm-dd'); v_five_years boolean;begin if months_between(sysdate,v_hire_date)/12 > 5 then v_five_years := true; dbms_output.put_line('true'); else v_five_years := false; dbms_output.put_line('false'); end if;end;/declare
v_year date := to_date('2000-10-10','yyyy-mm-dd'); v_five_years boolean;begin if months_between(sysdate,v_year)/12 > 5 then v_five_years := true; else v_five_years := false; end if; if v_five_years then dbms_output.put_line('yes'); else dbms_output.put_line('no'); end if;end;/使用CASE表达式:
set serveroutput ondefine p_grade='a'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;/DECLARE
v_grade CHAR(1) := UPPER('&p_grade'); v_appraisal VARCHAR2(20);BEGIN v_appraisal := CASE --省略 WHEN v_grade='A' THEN 'Excellent' WHEN v_grade='B' THEN 'Very Good' WHEN v_grade='C' THEN 'Good' ELSE 'No such grade' END; DBMS_OUTPUT.PUT_LINE ('Grade: '|| v_grade || ' Appraisal ' || v_appraisal);END;/set serveroutput ondeclare v_score number := &score; v_grade VARCHAR2(10) := 'no grade';begin v_grade := case to_char(trunc(v_score,-1)) when '90' then 'A' when '80' then 'B' when '70' then 'C' else 'D' end; dbms_output.put_line(v_grade);end;循环遍历控制:loop...end loop;while ... loop
....end loop;for ... loop
...end loop;1.基本LOOP循环:
set serveroutput ondeclare i integer :=1;begin loop dbms_output.put_line(i); i := i+1; exit when i>10; end loop;end;/while循环:
declare i integer :=1;begin while i<=10 loop dbms_output.put_line(i); i := i+1; end loop;end;/数字for循环:
for 后的变量类型 由in后面内容决定 如果后面是数值 变量就是数值类型 如果后面是select(游标) 变量就是 recordbegin for i in 1..10 loop dbms_output.put_line(i); end loop;end;/begin
for i in reverse 1..10 loop dbms_output.put_line(i); end loop;end;/实现双重循环
外循环和内循环都执行5次. 内外计数器变量名相同:i 显示内循环的计数器和外循环的计数器的乘积 当乘积超过15时候,退出declare v_plus number(10);begin <<outer_loop>> for i in 1..5 loop <<inner_loop>> for i in 1..5 loop v_plus:=i*outer_loop.i; exit when v_plus>15; dbms_output.put_line(v_plus); end loop inner_loop; end loop outer_loop;end;/标号和goto:
declare v_counter number := 1;begin loop dbms_output.put_line('in loop V_counter current value:'||V_counter); v_counter := v_counter + 1; if v_counter > 10 then goto l_ENDofLOOP; end if; end loop; <<l_ENDofLOOP>> dbms_output.put_line('end loop V_counter current value:'||V_counter);end;/写一个PL/SQL块
向dept表中循环插入5条记录 每一条记录的deptno 值比表中最大的deptno 值增加1 dname分别为"Test1" "Test2"..."Test5" loc列的值都为空。sys>create table hr.dept as select * from scott.dept;
hr> declare v_deptno hr.dept.deptno%type; begin select max(deptno) into v_deptno from hr.dept; for i in 1..5 loop insert into hr.dept values(v_deptno+i,'Test'||to_char(i),null); end loop; end;
事务处理控制语句(COMMIT 和 ROLLBACK):
CREATE TABLE T1(ID INT);(*批量提交,减少log file sync提高效率!)DECLARE V_NUM NUMBER := 0;BEGINFOR V_LOOPCOUNTER IN 1..500 LOOP INSERT INTO T1 VALUES (V_LOOPCOUNTER); V_NUM := V_NUM + 1; IF V_NUM = 50 THEN COMMIT; V_NUM := 0; END IF; END LOOP; COMMIT;END;/*********
drop table t1 purge;create table t1 (id number);begin
for i in 1..1000 loop insert into t1 values(i); if i mod 50 = 0 then commit; end if; end loop;end;Plsql table+record+循环打印结果集(dept表的所有行所有列)
declare TYPE CharacterTab IS TABLE OF dept%rowtype index by binary_integer; v_Character CharacterTab; v_max number; v_deptno number;begin select count(*) into v_max from dept; for i in 1..v_max loop select deptno into v_deptno from (select rownum rn,d.* from dept d) where rn=i; select * into v_Character(i) from dept where deptno=v_deptno; end loop; for i in 1..v_max loop dbms_output.put_line(v_Character(i).deptno||' '||v_Character(i).dname||' '||v_Character(i).loc); end loop;end;/declare
v_deptno hr.dept.deptno%type; v_dname hr.dept.dname%type; v_loc hr.dept.loc%type; v_max number;begin select count(*) into v_max from hr.dept; for i in 1..v_max loop select deptno,dname,loc into v_deptno,v_dname,v_loc from (select rownum rn,d.* from hr.dept d) where rn=i; dbms_output.put_line(v_deptno||' '||v_dname||' '||v_loc); end loop;end;/