摘要:
Common table expressions(CTE): CTE is a temporary named result set or view, and a CTE can reference itself, in which case it's called a recursive query. In other words, the CTE allows you to execute recursive (hierarchical) queries in a really easy way.
以前我们都是自己写很多的 T_SQL 代码来实现这样的关系图。在 "Yukon" 有了 CTE ,问题就好解决多了。 在Sql Server 2000里,我是通过操作临时表来实现这样的功能的,不知道还有没有别的方法来实现。
看看在'Yukon'中的代码吧:
WITH EmpCTE(empid, empname, mgrid, lvl, sortcol)AS( SELECT empid, empname, mgrid, 0,CAST(empid AS VARBINARY(900))FROM EmployeesWHERE empid = 1UNION ALLSELECT E.empid, E.empname, E.mgrid, M.lvl+1,CAST(sortcol + CAST(E.empid AS BINARY(4)) AS VARBINARY(900))FROM Employees AS EJOIN EmpCTE AS MON E.mgrid = M.empid)SELECTREPLICATE(' | ', lvl)+ '(' + (CAST(empid AS VARCHAR(10))) +......[
阅读全文]