无限分类存储过程

CREATE procedure deletecolumn(@nfather nvarchar(50))
as
declare @t table(n_name nvarchar(50),n_fathername nvarchar(50),level int)
declare @i int
set @i=1
insert into @t select n_name,n_fathername,@i from news where n_fathername=@nfather
while @@RowCount>0
begin
set @i=@i+1
insert into @t
select a.n_name,a.n_fathername,@i from news a left join @t b on a.n_fathername=b.n_name where b.level=@i-1
end
delete from news where n_name=@nfather
delete from news where n_name in (select n_name from @t)
GO
查询:

CREATE procedure gridview (@fathername nvarchar(50))
as
declare @t table(n_name nvarchar(50),n_father nvarchar(50),level int,Sort nvarchar(4000))
declare @i int
set @i=1
insert into @t select n_name,n_fathername,@i,n_name from news where n_fathername=@fathername
while @@Rowcount>0
begin
set @i=@i+1
insert into @t
select a.n_name,a.n_fathername,@i,b.Sort+a.n_name from news a left join @t b on a.n_fathername=b.n_name where

b.level=@i-1
end
select a.n_id,REPLICATE('-',b.level*2-2)+'├'+a.n_name,a.n_class,a.n_type,a.n_shunxu from news a left join @t b on

a.n_name=b.n_name order by b.Sort
GO

你可能感兴趣的:(存储过程)