递归查询,记录一下,

CREATE TABLE [abc](

[id] [int] NULL,
[pid] [int] NULL,
[name] [nchar](10)
)
GO
INSERT INTO [abc] VALUES(1,0,'a')
INSERT INTO [abc] VALUES(2,0,'b')
INSERT INTO [abc] VALUES(3,1,'c')
INSERT INTO [abc] VALUES(4,1,'d')
INSERT INTO [abc] VALUES(5,2,'e')
INSERT INTO [abc] VALUES(6,3,'f')
INSERT INTO [abc] VALUES(7,3,'g')
INSERT INTO [abc] VALUES(8,4,'h')
GO

--begin

with Dep as
(
select id,pid,[name] from abc where id=1
union all
select d.id,d.pid,d.[name] from Dep inner join abc d on Dep.id=d.pid
)
select * from Dep

-- end

上边是个整体的sql,开始以为后边的select是单独的呢

找的资料,自己记录一下