日期:2014-05-17 浏览次数:20546 次
declare @t table( 类别ID int, 类别名称 varchar(20), 上级类别ID int)
insert into @t
select 1,'父类1' ,0 union all
select 2,'子类11' ,1 union all
select 3,'子类12' ,1 union all
select 4,'子子类111',2 union all
select 5,'父类2' ,0
select * from @t t1
where not exists(select 1 from @t t2 where t1.类别ID=t2.上级类别ID)
declare @t table( 类别ID int, 类别名称 nvarchar(20), 上级类别ID int)
insert into @t
select 1,'父类1' ,0
union all select 2,N'子类11' ,1
union all select 3,N'子类12' ,1
union all select 4,N'子子类111',2
union all select 5,N'父类2' ,0
select * from @t a
where not exists(select 1 from @t where a.类别ID=上级类别ID)
/*
类别ID 类别名称 上级类别ID
----------- -------------------- -----------
3 子类12 1
4 子子类111 2
5 父类2 0
(3 row(s) affected)
CREATE TABLE t1