if exists(select * from sysobjects where name='tb_tree') drop table tb_tree go create table tb_tree ( id int primary key identity, name varchar(50), pId int, level int, sort int, cNum int ) go insert into tb_tree(name,pId,level,sort,cNum) values('节点1',0,1,0,6); insert into tb_tree(name,pId,level,sort,cNum) values('节点1-1',1,2,2,3); insert into tb_tree(name,pId,level,sort,cNum) values('节点1-1-1',2,3,3,0); insert into tb_tree(name,pId,level,sort,cNum) values('节点1-1-2',2,3,2,0); insert into tb_tree(name,pId,level,sort,cNum) values('节点1-1-3',2,3,1,0); insert into tb_tree(name,pId,level,sort,cNum) values('节点1-1-3-1',5,4,0,0); insert into tb_tree(name,pId,level,sort,cNum) values('节点1-2',1,2,1,2); insert into tb_tree(name,pId,level,sort,cNum) values('节点1-2-1',6,3,2,0); insert into tb_tree(name,pId,level,sort,cNum) values('节点1-2-2',6,3,1,0); --------------修改排序---------- if exists (select * from sysobjects where name='proc_tree_move') drop proc proc_tree_move go create proc proc_tree_move(@id int,@newpId int,@sibId int,@dir int)--假设@sibId=3 as declare @oldpId int,@oldsort int, @newSibsort int --@newSibsort:目标节点的排序值 select @oldpId=pId,@oldsort=sort from tb_tree where id=@id --修改原兄弟节点的排序,后面的不动,前面的全部减1 例如:5,4,3,2,1 将3移走 5,4都减去1 就变成了 4,3,2,1 update tb_tree set sort=sort-1 where pId=@oldpId and sort>@oldsort if(@sibId<>0)--判断是否选中了目标节点 begin --获得目标节点的排序(排序靠前或靠后:@dir=-1:表示移动到目标节点后面,@dir=1:表示移动到目标节点的前面) select @newSibsort=sort from tb_tree where id=@sibId --修改目标兄弟节点的排序 只修改排序大于等于目标节点的值的 如:原排序:5,4,3,2,1 --情况1 :将一个节点插入到目标节点3的 "前" 面,则5,4排序值加1,就变成了 6,5,(插入值),3(表目标节点),2,1 --情况2 :将一个节点插入到目标节点3的 "后" 面,则5,4,3排序值加1,就变成了 6,5,4(目标节点),(插入值),2,1 if(@dir>0)--前面 begin update tb_tree set sort=sort+1 where pId=@newpId and sort>=@newSibsort+1 --前面(@newSibsort=3) end else--后面 begin update tb_tree set sort=sort+1 where pId=@newpId and sort>=@newSibsort --后面(@newSibsort=3) end --重新给目标节点赋值(前面(@dir=1):目标节点排序不变等于3,后面(@dir=0):目标节点排序加1等于4) select @newSibsort=sort from tb_tree where id=@sibId --修改自己的排序 update tb_tree set sort=@newSibsort+@dir where id=@id end else begin --修改自己的排序 update tb_tree set sort=0 where id=@id end go --修改父节点子节点数量 --修改兄弟节点的排序 --修改自身及其子节点的深度 if exists (select * from sysobjects where name='proc_tree_level') drop proc proc_tree_level go create proc proc_tree_level(@id int,@level int) as declare @count int,@tmpId int select @count=COUNT(1) from tb_tree where pId=@id if(@count<>0)--判断是否有子节点 begin declare cur_level cursor local for select id from tb_tree where pId=@id open cur_level fetch next from cur_level into @tmpId while(@@FETCH_STATUS=0) begin exec proc_tree_level @tmpId,@level fetch next from cur_level into @tmpId end close cur_level deallocate cur_level end update tb_tree set level=level+@level where id=@id go --修改父节点的子节点个数 if exists (select * from sysobjects where name='proc_tree_cNum') drop proc proc_tree_cNum go create proc proc_tree_cNum(@pId int,@cNum int) as declare @tmpId int select @tmpId=pId from tb_tree where id=@pId if(@tmpId<>0) begin exec proc_tree_cNum @tmpId,@cNum end update tb_tree set cNum=cNum+@cNum where id=@pId go --添加节点 if exists (select * from sysobjects where name='proc_tree_add') drop proc proc_tree_add go create proc proc_tree_add(@name varchar(50),@pId int) as declare @pLevel int,@pCNum int select @pLevel=level,@pCNum=cNum from tb_tree where id=@pId begin try begin tran insert into tb_tree(name,pId,level,sort,cNum) values(@name,@pId,@pLevel+1,0,0);--添加节点 exec proc_tree_cNum @pId,1--修改父节点的子节点个数 commit tran end try begin catch rollback tran end catch go select * from tb_tree --exec proc_tree_add '节点1-1-4',2 --exec proc_tree_cNum 4,1 --exec proc_tree_level 1,2; exec proc_tree_move 6,1,2,-1; select * from tb_tree