首页 > 数据库 > 数据库开发技术 > PLSQL学习-【3循环结构】
分批提交例子:
declare cursor cu_emp is select * from emp1; begin for v_cu in cu_emp loop if v_cu.ename = \'haha\' then delete from emp where empno=v_cu.empno; end if; if mod(cu_emp%rowcount,1000)=0 then dbms_output.put_line(cu_emp%rowcount); commit; end if; end loop; -- commit; end; / PL/SQL procedure successfully completed.
CLERK部门的员工,津贴变成200:
10:32:59 SQL> declare 10:33:11 2 cursor cu_emp is select * from emp; 10:33:23 3 begin 10:33:24 4 for i in cu_emp loop 10:33:35 5 if i.job='CLERK' then 10:33:51 6 update emp set comm = 200 where empno = i.empno; 10:34:11 7 end if; 10:34:19 8 end loop; 10:34:24 9 --commit; 10:34:30 10 end; 10:34:32 11 /
declare cursor cu_emp is select * from emp; begin for i in cu_emp loop if i.job = 'CLERK' then update emp set comm = 200 where empno = i.empno; elsif i.job = 'SALESMAN' then --日语钱包:saiif 发音很像 update emp set comm = 1000 where empno = i.empno; end if; end loop; end; /
第一次执行时间102s:
第二次执行耗时很短:
三种循环:
loop:
declare i number; begin i:=1; loop insert into emp(empno,ename) values(i,'YAO' || i); i:=i+1; exit when i > 2000; end loop; commit; end;
while:
declare i number; begin i:=3000; while i<=6000 loop insert into emp(empno,ename) values(i,\'YAO\' || i); i:=i+1; end loop; commit; end;
for循环:
declare begin for i in 8004..9999 loop insert into emp(empno,ename) values (i,'LOU'); end loop; commit; end; /
循环嵌套:
打印9 9 乘法表
12:59:19 SQL> declare 12:59:26 2 begin 12:59:29 3 for i in 1..9 loop 12:59:38 4 for j in 1..9 loop 12:59:54 5 dbms_output.put_line(i||'*'||j ||'='||i*j); 13:01:33 6 end loop; 13:01:42 7 end loop; 13:01:46 8 end; 13:01:49 9 / set serveroutput on
循环退出:
declare begin <> --标签 for i in 1..9 loop < > for j in 1..9 loop exit outer when j > 5; dbms_output.put_line(i||'*'||j||'='||i*j); end loop; end loop; end;
直接调到外层:
declare
begin
<>
for i in 1..9 loop
<>
for j in 1..9 loop
exit outer when j > 5;
dbms_output.put_line(i||\'*\'||j||\'=\'||i*j);
end loop;
end loop;
end;
1*1=1
1*2=2
1*3=3
1*4=4
1*5=5
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29990276/viewspace-1339263/ ,如需转载,请注明出处,否则将追究法律责任。