程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> DB2數據庫 >> DB2教程 >> DB2嵌套游標

DB2嵌套游標

編輯:DB2教程
p1:begin
   declare v_did varchar(10);
   declare v_dname varchar(10);
   declare v_tid varchar(10);
   declare v_tname varchar(10);
   declare i_seq integer default 0;
  declare not_found condition for sqlstate ''02000'';
  declare at_endc1 int default 0;
  declare at_endc2 int default 0;
  p2: begin  -- open department
    declare cursor1 cursor for
          select did,dname from department;
    declare continue handler for not_found set at_endc1 = 1;
   open cursor1;
  --遍歷 cursor1
  while(at_endc1 < 1) do
    fetch cursor1 into v_did,v_dname;
    p3:begin
 declare cursor2 cursor for
      select ID, NAME from teacher where did=v_did and dname=v_dname;
    declare continue handler for not_found set at_endc2 = 1;
  open cursor2;
  if (at_endc1 = 0) then
    set at_endc2 = 0;
  end if;
     --遍歷 cursor2
     while(at_endc2 < 1) do
    fetch cursor2 into v_tid,v_tname;
    insert into student(ID, NAME, TID, TNAME, DID, DNAME) values(i_seq,char(i_seq),v_tid,v_tname,v_did,v_dname);
       set i_seq=i_seq+1;
     end while;
  close cursor2; 
    end p3;
  end while;
  close cursor1;
  end p2;
END p1 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved