我可以在SQL中生成递归查询吗? [英] Can I generate a recursive query in SQL?
本文介绍了我可以在SQL中生成递归查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如:
我有一个表,其中维护着所有员工及其所报告的员工.现在,如果我想知道根据单个雇员向谁报告的人,我该如何递归地获得所有雇员的名单.
For example:
I have a table in which I maintain all employees and the employees they report to. Now, If I want to know who is report to whom based on a single employees, how can I achieve that list of all employees recursively.
Table Structure is TABLE : (EmpId, ReportTo)
Employee 1 is report to none, ReportTo = 0
Employee 2 is Reporting to Employee 1, ReportTo = 1
Employee 3 is Reporting to Employee 1, ReportTo = 1
Employee 4 is Reporting to Employee 2, ReportTo = 2
Employee 5 is Reporting to Employee 2, ReportTo = 2
Employee 6 is Reporting to Employee 4, ReportTo = 4
Employee 7 is Reporting to Employee 5, ReportTo = 5
现在,如果我想递归地知道
的所有下属的链
员工1.通过查询怎么可能?
任何帮助将非常感激...
问候,
Jawwad!
Now If I want to know recursively the chain of all the subordinates of
Employee 1. How would that be possible by query?
Any help would be highly thankful...
Regards,
Jawwad!
推荐答案
请参阅以下有关SQL递归的文章;
http://msdn.microsoft.com/en-us/library/ms186243.aspx [ ^ ]
它有一个您想要的雇员示例.
See the following article on SQL Recursion;
http://msdn.microsoft.com/en-us/library/ms186243.aspx[^]
It has an Employee Example like you want.
是的,您可以轻松完成sql server 2005 ....
yes you cane do it easyli is sql server 2005....
这篇关于我可以在SQL中生成递归查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文