开发者

Parent-child relationship showing through simple SQL in SQL Server 2000

Suppose my table structure is

EmployeeID
Name
ManagerID

Employee     ReportTo 
-----------------------
ANA           BEN
KIN           ANA
ARI           NULL
BEN           NULL

So please tell me how could I show this type of output writing simple SQL开发者_Go百科 in SQL Server 2000.


As far as I can see you only want the direct reports - so a recursive solution isn't needed:

select em.Name as Employee, mg.Name as ReportTo
from dbo.tYourTable em
left join dbo.tYourTable mg
 on mg.EmployeeID = em.ManagerID;

If you need a recursive solution you will only find procedural solutions or solutions with a limited depth.


Before SQL Server 2005, you need a recursive udf

I don't have SQL Server 2000 to test a solution on (I've done it years ago) but here are two articles from the Interwebs:

  • http://weblogs.sqlteam.com/jeffs/archive/2003/11/21/588.aspx
  • http://www.paragoncorporation.com/ArticleDetail.aspx?ArticleID=9
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜