db2資料庫儲存過程入門6

2021-06-09 16:38:23 字數 1872 閱讀 8121

例8:

--在儲存過程中用while迴圈進行遞迴查詢sid分類下的所有子分類

drop procedure test14;

create procedure test14(in sid integer)

language sql

begin

declare num        integer;--子分類的數量

declare global temporary table session.c_dtdoctype--子分類的臨時表

(id integer,

parentid integer,

name varchar(300)

);declare global temporary table session.p_dtdoctype--父分類的臨時表

(id integer,

parentid integer,

name varchar(300)

);declare global temporary table session.r_dtdoctype--結果分類的臨時表

(id integer,

parentid integer,

name varchar(300)

)with replace;

--查詢表dtdoctype中的資料,插入到臨時表session.dtdoctype中

insert into session.p_dtdoctype select id,parentid,name from dtdoctype where parentid = sid;

insert into session.r_dtdoctype select id,parentid,name from dtdoctype where parentid = sid;

select count(*) into num from session.p_dtdoctype;--查詢父表中是否有資料

while(num>0)

do delete from session.c_dtdoctype;--刪除子表中的資料

--從父表中查出子分類集合存放到子表中

insert into session.c_dtdoctype select id,parentid,name from dtdoctype where parentid in (select id from session.p_dtdoctype);

insert into session.r_dtdoctype select id,parentid,name from dtdoctype where parentid in (select id from session.p_dtdoctype);

--從父表中查出子分類集合存放到子表中

delete from session.p_dtdoctype;--刪除父表中的資料

--將子表中的資料存放在父表中

insert into session.p_dtdoctype select * from session.c_dtdoctype;

--查詢父表中是否有資料

select count(*) into num from session.p_dtdoctype;

end while;

begin

declare ydtdoctype cursor with return for select * from session.r_dtdoctype;

open ydtdoctype;--開啟游標

end;

drop table session.c_dtdoctype;

drop table session.p_dtdoctype;

--drop table session.r_dtdoctype;

end;

db2儲存過程批量插入資料 db2儲存過程批量更新

db2儲存過程批量更新 雲伺服器 elastic compute service,簡稱ecs 是阿里雲提供的效能卓越 穩定可靠 彈性擴充套件的iaas infrastructure as a service 級別雲計算服務。雲伺服器ecs免去了您採購it硬體的前期準備,讓您像使用水 電 天然氣等公共...

db2儲存過程批量插入資料 db2儲存過程批量提交

db2儲存過程批量提交 雲伺服器 elastic compute service,簡稱ecs 是阿里雲提供的效能卓越 穩定可靠 彈性擴充套件的iaas infrastructure as a service 級別雲計算服務。雲伺服器ecs免去了您採購it硬體的前期準備,讓您像使用水 電 天然氣等公共...

DB2儲存過程雜談

db2常用的異常處理方式分為三種 undo 回滾 exit 退出 continue 繼續執行 declare undo exit continue handler for 異常處理的物件可以為針對指定的sqlstate declare undo exit continue handler for s...