树形结构的处理--数据复制
2009-01-08 10:53
363 查看
--数据结构
表名tb,如果修改表名,则相应修改所有数据处理中涉及到的表名tb
id为编号(标识字段+主键)
pid为上级编号
name为名称,后面可以自行增加其他字段.
/*--数据复制
如果表中包含自定义字段,需要修改存储过程
存在嵌套不超过32层的问题.
--*/
--创建复制的存储过程--复制指定结点下的子结点到另一个结点下
create proc p_copy
@s_id int, --复制该项下的所有子项
@d_id int, --复制到此项下
@new_id int --新增加项的开始编号
as
declare @nid int,@oid int,@name varchar(20)
select id,name into #temp from tb where pid=@s_id and id<@new_id
while exists(select 1 from #temp)
begin
select @oid=id,@name=name from #temp
insert into tb values(@d_id,@name)
set @nid=@@identity
exec p_copy @oid,@nid,@new_id
delete from #temp where id=@oid
end
go
--创建批量复制的存储过程--复制指定结点及其下面的所有子结点,并生成新结点
create proc p_copystr
@s_id varchar(8000) --要复制项的列表,用逗号分隔
as
declare @nid int,@oid int,@name varchar(20)
set @s_id=','+@s_id+','
select id,name into #temp from tb
where charindex(','+cast(id as varchar)+',', @s_id)>0
while exists(select 1 from #temp)
begin
select @oid=id,@name=name from #temp
insert into tb values(@oid,@name)
set @nid=@@identity
exec p_copy @oid,@nid,@nid
delete from #temp where id=@oid
end
go
--测试
exec p_copystr '5,6'
--显示处理结果
select * from tb order by dbo.f_getmergid(id)
go
表名tb,如果修改表名,则相应修改所有数据处理中涉及到的表名tb
id为编号(标识字段+主键)
pid为上级编号
name为名称,后面可以自行增加其他字段.
/*--数据复制
如果表中包含自定义字段,需要修改存储过程
存在嵌套不超过32层的问题.
--*/
--创建复制的存储过程--复制指定结点下的子结点到另一个结点下
create proc p_copy
@s_id int, --复制该项下的所有子项
@d_id int, --复制到此项下
@new_id int --新增加项的开始编号
as
declare @nid int,@oid int,@name varchar(20)
select id,name into #temp from tb where pid=@s_id and id<@new_id
while exists(select 1 from #temp)
begin
select @oid=id,@name=name from #temp
insert into tb values(@d_id,@name)
set @nid=@@identity
exec p_copy @oid,@nid,@new_id
delete from #temp where id=@oid
end
go
--创建批量复制的存储过程--复制指定结点及其下面的所有子结点,并生成新结点
create proc p_copystr
@s_id varchar(8000) --要复制项的列表,用逗号分隔
as
declare @nid int,@oid int,@name varchar(20)
set @s_id=','+@s_id+','
select id,name into #temp from tb
where charindex(','+cast(id as varchar)+',', @s_id)>0
while exists(select 1 from #temp)
begin
select @oid=id,@name=name from #temp
insert into tb values(@oid,@name)
set @nid=@@identity
exec p_copy @oid,@nid,@nid
delete from #temp where id=@oid
end
go
--测试
exec p_copystr '5,6'
--显示处理结果
select * from tb order by dbo.f_getmergid(id)
go
相关文章推荐
- JSON复杂数据处理之Json树形结构数据转Java对象并存储到数据库的实现
- 树形结构的处理--数据新增,修改、删除
- 树形此博文包含图片结构数据后台处理:一次循环生成树
- 树形结构数据后台处理:一次循环生成树
- 树形结构数据后台处理:一次循环生成树
- Json树形结构数据转Java对象并存储到数据库的实现-超简单的JSON复杂数据处理 .
- 树形结构的处理——组合模式(二):组合模式概述
- 树形结构的处理——组合模式(一)
- sql复制表结构和数据
- 7. Orcle树形结构(类似数据字典有父子类关系),查询末节点的所有记录
- 树形结构的处理——组合模式(五):公司组织结构,组合模式总结
- Oracle Class6-1. PL/SQL 简介(数据类型,逻辑比较,控制结构,错误处理)
- 如何在Oracle中复制表结构和表数据
- sql 复制表结构 不复制数据
- libnids-1.21 中 IP 分片重组分析 之数据结构与处理流程
- oracle复制表数据,复制表结构
- java拼接树形结构数据
- .net前端后台两种方式处理树形结构(tree)
- ARM Linux对中断的处理--相关数据结构
- js将有父子关系的数据转换成树形结构数据