db2数据库存储过程入门6
来源:互联网 发布:安全联盟认证知乎 编辑:程序博客网 时间:2024/05/16 16:08
例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数据库存储过程入门6
- db2数据库存储过程入门1
- db2数据库存储过程入门2
- db2数据库存储过程入门3
- db2数据库存储过程入门4
- db2数据库存储过程入门5
- db2数据库存储过程入门7
- db2数据库存储过程入门8
- DB2存储过程入门
- DB2存储过程入门实例
- DB2存储过程快速入门
- DB2存储过程入门实例
- DB2存储过程快速入门
- DB2纯SQL存储过程入门实践
- DB2存储过程入门(一)
- db2 创建数据库、表、存储过程等
- DB2数据库使用存储过程详解
- MySQL---数据库存储过程入门
- hdu1558 Segment set
- 安装sql2000时提示:“写时无法打开指定的文件。请确保该文件没有使用,然后重新启动安装程序” .
- 羌笛声声胡笳怨,此心长寄天涯远
- Linux 用户态与内核态的交互 ——netlink 篇
- db2数据库存储过程入门5
- db2数据库存储过程入门6
- 腾讯QQ邮箱漏洞--修改封包欺骗邮件服务器
- XAF之Frame和Controller
- JNI学习-C 调用Java
- Xmemcached使用之与Spring整合
- 淡淡的微笑,静悄悄地过
- protobuf-net简单示例
- 第一次写博客,做好自己
- db2数据库存储过程入门7