- SQL>set serveroutput on; --顯示輸出
- -------------------------------------------------
- --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;
- ------------
PL/SQL入門
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.