<script type="text/javascript">
</script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
描述:讨论如何 处理 树形数据,排序,新增,修改,复制,删除,数据完整性检查,汇总统计
表结构描述及数据环境:
表名tb,如果修改表名,则相应修改所有数据 处理中涉及到的表名tb
id为编号(标识字段+主键),pid为上级编号,name为名称,后面可以自行增加其他字段.
凡是未特殊标注的地方,对自行增加的字段不影响 处理结果/*--数据测试环境
表名tb,如果修改表名,则相应修改所有数据 处理中涉及到的表名tb
id为编号(标识字段+主键)
pid为上级编号
name为名称,后面可以自行增加其他字段.
凡是未特殊标注的地方,对自行增加的字段不影响 处理结果
--表环境
createtabletb(idintidentity(1,1)notnullconstraintPK_tbprimarykeyclustered
,pidint,namevarchar(20))
insertintotb
select0,'中国'
unionallselect0,'美国'
unionallselect0,'加拿大'
unionallselect1,'北京'
unionallselect1,'上海'
unionallselect1,'江苏'
unionallselect6,'苏州'
unionallselect7,'常熟'
unionallselect6,'南京'
unionallselect6,'无锡'
unionallselect2,'纽约'
unionallselect2,'旧金山'
go
-- 处理中需要使用的函数及存储过程
--1.自定义函数--获取编码累计
createfunctionf_getmergid(@idint)
returnsvarchar(8000)
as
begin
declare@revarchar(8000),@pidint
--为了数字排序正常,需要统一编码宽度
declare@idlenint,@idheadervarchar(20)
select@idlen=max(len(id))
,@idheader=space(@idlen)
fromtb
--得到编码累计
set@re=right(@idheader+cast(@idasvarchar),@idlen)
select@pid=pidfromtbwhereid=@id
while@@rowcount>0
select@re=right(@idheader+cast(@pidasvarchar),@idlen)+','+@re
,@pid=pidfromtbwhereid=@pid
return(@re)
end
go
--2.自定义函数--检测某个编码出发,是否被循环引用
createfunctionf_chkid(@idint)
returnsbit --循环,返回1,否则返回0
as
begin
declare@rebit,@pidint
set@re=0
--检测
select@pid=pidfromtbwhereid=@id
while@@rowcount>0
begin
if@pid=@id
begin
set@re=1
gotolbErr
end
select@pid=pidfromtbwhereid=@pid
end
lbErr:
return(@re)
end
go
/*--数据复制
如果表中包含自定义字段,需要修改存储过程
存在嵌套不超过32层的问题.
--*/
--3.复制指定结点下的子结点到另一个结点下
createprocp_copy
@s_idint, --复制该项下的所有子项
@d_idint, --复制到此项下
@new_idint --新增加项的开始编号
as
declare@nidint,@oidint,@namevarchar(20)
selectid,nameinto#tempfromtbwherepid=@s_idandid<@new_id
whileexists(select1from#temp)
begin
select@oid=id,@name=namefrom#temp
insertintotbvalues(@d_id,@name)
set@nid=@@identity
execp_copy@oid,@nid,@new_id
deletefrom#tempwhereid=@oid
end
go
--4.批量复制的存储过程--复制指定结点及其下面的所有子结点,并生成新结点
createprocp_copystr
@s_idvarchar(8000) --要复制项的列表,用逗号分隔
as
declare@nidint,@oidint,@namevarchar(20) 1 <script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript">
</script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
描述:讨论如何 处理 树形数据,排序,新增,修改,复制,删除,数据完整性检查,汇总统计
表结构描述及数据环境:
表名tb,如果修改表名,则相应修改所有数据 处理中涉及到的表名tb
id为编号(标识字段+主键),pid为上级编号,name为名称,后面可以自行增加其他字段.
凡是未特殊标注的地方,对自行增加的字段不影响 处理结果/*--数据测试环境
表名tb,如果修改表名,则相应修改所有数据 处理中涉及到的表名tb
id为编号(标识字段+主键)
pid为上级编号
name为名称,后面可以自行增加其他字段.
凡是未特殊标注的地方,对自行增加的字段不影响 处理结果
--表环境
createtabletb(idintidentity(1,1)notnullconstraintPK_tbprimarykeyclustered
,pidint,namevarchar(20))
insertintotb
select0,'中国'
unionallselect0,'美国'
unionallselect0,'加拿大'
unionallselect1,'北京'
unionallselect1,'上海'
unionallselect1,'江苏'
unionallselect6,'苏州'
unionallselect7,'常熟'
unionallselect6,'南京'
unionallselect6,'无锡'
unionallselect2,'纽约'
unionallselect2,'旧金山'
go
-- 处理中需要使用的函数及存储过程
--1.自定义函数--获取编码累计
createfunctionf_getmergid(@idint)
returnsvarchar(8000)
as
begin
declare@revarchar(8000),@pidint
--为了数字排序正常,需要统一编码宽度
declare@idlenint,@idheadervarchar(20)
select@idlen=max(len(id))
,@idheader=space(@idlen)
fromtb
--得到编码累计
set@re=right(@idheader+cast(@idasvarchar),@idlen)
select@pid=pidfromtbwhereid=@id
while@@rowcount>0
select@re=right(@idheader+cast(@pidasvarchar),@idlen)+','+@re
,@pid=pidfromtbwhereid=@pid
return(@re)
end
go
--2.自定义函数--检测某个编码出发,是否被循环引用
createfunctionf_chkid(@idint)
returnsbit --循环,返回1,否则返回0
as
begin
declare@rebit,@pidint
set@re=0
--检测
select@pid=pidfromtbwhereid=@id
while@@rowcount>0
begin
if@pid=@id
begin
set@re=1
gotolbErr
end
select@pid=pidfromtbwhereid=@pid
end
lbErr:
return(@re)
end
go
/*--数据复制
如果表中包含自定义字段,需要修改存储过程
存在嵌套不超过32层的问题.
--*/
--3.复制指定结点下的子结点到另一个结点下
createprocp_copy
@s_idint, --复制该项下的所有子项
@d_idint, --复制到此项下
@new_idint --新增加项的开始编号
as
declare@nidint,@oidint,@namevarchar(20)
selectid,nameinto#tempfromtbwherepid=@s_idandid<@new_id
whileexists(select1from#temp)
begin
select@oid=id,@name=namefrom#temp
insertintotbvalues(@d_id,@name)
set@nid=@@identity
execp_copy@oid,@nid,@new_id
deletefrom#tempwhereid=@oid
end
go
--4.批量复制的存储过程--复制指定结点及其下面的所有子结点,并生成新结点
createprocp_copystr
@s_idvarchar(8000) --要复制项的列表,用逗号分隔
as
declare@nidint,@oidint,@namevarchar(20) 1 <script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript">
</script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>