------------------------------------------------- --LOOP循环 declare i number:=1; begin loop i:=i+1; exit when i=100; dbms_output.put_line(i); end loop;
end;
------------------------------------------------- -- FOR LOOP循环50次 declare i number :=10; begin for idx in 1..50 loop --次数:50-1+1=50 dbms_output.put_line(i); end loop; end;
------------------------------------------------- -- WHILE LOOP循环 declare i number :=100; j number :=1; begin while(j<i) loop dbms_output.put_line(j); j:=j+1; end loop; end;
------------------------------------------------- --IF语句 declare i number :=40; j number :=10; begin if(i<j) then dbms_output.put_line('OK'); elsif (i=j) then dbms_output.put_line('OK1'); else dbms_output.put_line('OK2'); end if; end;
--显示游标 declare --建立显示游标 cursor csr_1 is select name from stu1; cursor csr_2 is select id from stu1; --定义变量 str varchar2(10); str2 varchar2(10); begin --打开游标 open csr_1; open csr_2; --附值 fetch csr_1 into str; fetch csr_2 into str2; --输出 dbms_output.put_line(str); dbms_output.put_line(str2); end;
------------------------------------------------- --取表某列全部记录(适合控制) declare --建立显示游标 cursor csr_1 is select name,note from stu1; --定义变量 str varchar2(10); str2 varchar2(10); begin --打开游标 open csr_1; loop fetch csr_1 into str,str2; exit when csr_1%notfound; --到达记录尾 dbms_output.put_line(str || str2); end loop;
--关闭游标 close csr_1; end;
------------------------------------------------- --FOR LOOP查询表全部记录(简单--打开、附值、关闭隐式完成) declare --建立显示游标 cursor csr_1 is select name,note from stu1; begin for idx in csr_1 loop dbms_output.put_line(idx.name || idx.note); end loop; end;
------------------------------------------------- --利用游标复制表(效率不高) declare cursor csr_1 is select ks_xm,ks_zkz,bk_cj from ahzk_all_cj; i number; begin for idx in csr_1 loop insert into tmp values(idx.ks_xm,idx.ks_zkz,idx.bk_cj); i:=csr_1%rowcount; --获取当前记录数 if(i mod 1000=0) then dbms_output.put_line(i); commit; end if; end loop; commit; end;
--附:高效复制表 declare begin insert into tmp select ks_xm,ks_zkz,bk_cj from ahzk_all_cj; if(sql%found) then dbms_output.put_line('插入' || sql%rowcount || '行记录'); end if; commit; end; ------------