点缀's Blog

用.NET点缀我们的生活
随笔 - 44, 评论 - 378, 引用 - 84

导航

工具

关于

脊柱是我们这种生命的重要特征,在此基础上我们才有了光芒的智慧和丰富的情感。上帝赋予我们自由的意志,同时也赋予我们选择的重担。

标签

每月存档

广告



访客

 
 

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 Employees
WHERE empid = 1

UNION ALL

SELECT E.empid, E.empname, E.mgrid, M.lvl+1,
CAST(sortcol + CAST(E.empid AS BINARY(4)) AS VARBINARY(900))
FROM Employees AS E
JOIN EmpCTE AS M
ON E.mgrid = M.empid
)
SELECT
REPLICATE(' | ', lvl)
+ '(' + (CAST(empid AS VARCHAR(10))) + ') '
+ empname AS empname
FROM EmpCTE
ORDER BY sortcol

(1) Nancy
| (2) Andrew
| | (5) Steven
| | 恶魔的脸 Michael
| (3) Janet
| | (7) Robert
| | | (11) David
| | | | (14) James
| | | (12) Ron
| | | (13) Dan
| | 音符 Laura
| | (9) Ann
| (4) Margaret
| | (10) Ina

随贴广告(测试期)
相关文章

打印 | 张贴于 2003-11-03 16:14:00 | Tag:Sql Server

留言反馈

回复: T-SQL 在 'Yukon' 中的加强 编辑
YUKON可以直接支持C#或者VB.NET来写存储过程,T-SQL加不加强都无所谓了
2003-11-03 17:06:00 | [匿名用户:5drush]
回复: T-SQL 在 'Yukon' 中的加强 编辑
这个东东在oracle8就已经有了……
2003-11-03 16:55:00 | [匿名用户:大鱼儿]
博客主人设置本博客不允许匿名用户发表言论,请登录后再试

Powered by: Joycode MVC Blogger System