SqlServer递归查询
SQL Server 2005开始支持递归查询了。 之前我们在保存一个树状结构的时候,常常采用在表格中增加一个ParentID这个字段保存其对应上级,但是这样的表格设计,在查询的时候,需要多次查询才能查询出所需要的结果,一般都需要程序来实现。 现在不用了,SQL Server 2005支持递归查询了(Oracle和DB2早支持了)。 写法如下: 假设表中的上下级关系采用的是ID和ParentID字段来关联。 with myT2 as( with myT2 as( select * from Sys_Organization Where sOrgCode='001' ?union all select Sys_Organization.* from myT2 inner join Sys_Organization on myT2.sOrgCode=Sys_Organization.sParentOrgCode) select * from HR_Employee_Info where sOrgCode in (select sOrgCode from myT2) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |