{"id":1898,"date":"2011-08-24T10:22:11","date_gmt":"2011-08-24T02:22:11","guid":{"rendered":"http:\/\/jason695.why3s.tw\/wordpress\/?p=1898"},"modified":"2011-08-24T10:22:11","modified_gmt":"2011-08-24T02:22:11","slug":"%e8%bd%89%e8%b2%bc%e9%97%9c%e6%96%bccte","status":"publish","type":"post","link":"https:\/\/jason695.why3s.tw\/wordpress\/2011\/08\/24\/%e8%bd%89%e8%b2%bc%e9%97%9c%e6%96%bccte\/","title":{"rendered":"[\u8f49\u8cbc]\u95dc\u65bcCTE"},"content":{"rendered":"<p><a id=\"viewpost_ascx_TitleUrl\" href=\"http:\/\/www.dotblogs.com.tw\/dc690216\/archive\/2010\/02\/02\/13440.aspx\">SQL &#8211; \u4f7f\u7528 \u4e00\u822c\u8cc7\u6599\u8868\u904b\u7b97\u5f0f CTE (Common Table Expression)<\/a><\/p>\n<p>\u65e9\u671f\u53ef\u4ee5\u900f\u904e\u66ab\u5b58\u8cc7\u6599 \u8868(# \u6216 ##)\u6216\u8005 table \u4f86\u5b58\u653e\u8cc7\u6599\uff0c\u73fe\u5728\u4e5f\u53ef\u4ee5\u4f7f\u7528 \u4e00\u822c\u8cc7\u6599\u8868\u904b\u7b97\u5f0f CTE (Common Table Expression) \u4f86\u66ab\u5b58\u8cc7\u6599\u3002\u96d6\u7136 CTE \u7684\u67e5\u8a62\u662f\u4f7f\u7528 Select \u67e5\u8a62\uff0c\u4f46\u662f\u4f9d\u7167 MSDN \u7684\u8aaa\u660e\uff0c\u4ecd\u6709\u4e00\u4e9b\u9650\u5236\uff0c\u90a3\u5c31\u662f\u5728 CTE \u4e2d\u4e0d\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7684\u5b50\u53e5\uff1a<\/p>\n<p>1.COMPUTE \u6216 COMPUTE BY<br \/>\n2.ORDER BY (\u9664\u975e\u6307\u5b9a\u4e86 TOP \u5b50\u53e5)<br \/>\n3.INTO<br \/>\n4.\u542b\u6709\u67e5\u8a62\u63d0\u793a\u7684 OPTION \u5b50\u53e5<br \/>\n5.FOR XML<br \/>\n6.FOR BROWSE<\/p>\n<p>\u4ee5\u4e0b\u5c31\u4f86\u5be6\u4f5c\u4e00\u4e9b CTE \u7684\u7bc4\u4f8b&#8230;<\/p>\n<p>\u7bc4\u4f8b\u4e00\uff1a\u55ae\u4e00\u7684 CTE \u7528\u6cd5<\/p>\n<p>WITH OrdersTable (\u8a02\u55aeID, \u5ba2\u6236ID, \u54e1\u5de5ID) as<br \/>\n(<br \/>\nSelect OrderID, CustomerID, EmployeeID<br \/>\nfrom dbo.Orders<br \/>\n)<\/p>\n<p>Select *<br \/>\nFrom OrdersTable<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/4.blog.xuite.net\/4\/c\/8\/5\/14458933\/blog_223897\/txt\/30614743\/0.jpg?resize=187%2C236\" alt=\"\" width=\"187\" height=\"236\" border=\"0\" \/><\/p>\n<p>\u7bc4\u4f8b\u4e8c\uff1a\u5169\u500b CTE \u4f5c Join<\/p>\n<p>WITH OrdersTable (\u8a02\u55aeID, \u5ba2\u6236ID, \u54e1\u5de5ID) as<br \/>\n(<br \/>\nSelect OrderID, CustomerID, EmployeeID<br \/>\nfrom dbo.Orders<br \/>\n),<br \/>\nOrderDetailsTable as<br \/>\n(<br \/>\nSelect OrderID as &#8216;\u8a02\u55aeID&#8217;, ProductID as &#8216;\u7522\u54c1ID&#8217;, UnitPrice as &#8216;\u50f9\u683c&#8217;<br \/>\nfrom dbo.[Order Details]<br \/>\n)<\/p>\n<p>Select *<br \/>\nFrom OrdersTable A inner join OrderDetailsTable B<br \/>\non A.\u8a02\u55aeID = B.\u8a02\u55aeID<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/4.blog.xuite.net\/4\/c\/8\/5\/14458933\/blog_223897\/txt\/30614743\/1.jpg?resize=317%2C272\" alt=\"\" width=\"317\" height=\"272\" border=\"0\" \/><\/p>\n<p>\u7bc4\u4f8b\u4e09\uff1aCTE \u905e\u8ff4 (\u53c3\u8003\uff1a\u9ed1\u6697\u5927\u7684\u90e8\u843d\u683c\u6587\u7ae0)<\/p>\n<p>\u8cc7\u6599\u6e05\u55ae<br \/>\n<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/4.blog.xuite.net\/4\/c\/8\/5\/14458933\/blog_223897\/txt\/30614743\/2.jpg?resize=218%2C277\" alt=\"\" width=\"218\" height=\"277\" border=\"0\" \/><\/p>\n<p>Code\uff1a<\/p>\n<p>&#8211;\u5982\u679cCTE\u7684WITH\u4e0d\u5728\u7b2c\u4e00\u5217, \u524d\u65b9\u8981\u52a0\u4e0a;<br \/>\n;WITH \u516c\u53f8\u7d44\u7e54\u7d50\u679c(\u90e8\u9580\u540d\u7a31, \u76f4\u5c6c\u90e8\u9580, \u5c64\u7d1a, \u6392\u5e8f\u6b04\u4f4d)<br \/>\nAS<br \/>\n(<br \/>\n&#8211;Recursive CTE\u5206\u70ba\u5169\u500b\u90e8\u5206, \u7b2c\u4e00\u90e8\u5206\u70baAnchor Member<br \/>\n&#8211;\u6307\u4e0d\u6703\u88ab\u905e\u8ff4\u547c\u53eb\u5230\u7684\u90e8\u5206<br \/>\nSELECT \u90e8\u9580\u540d\u7a31, \u76f4\u5c6c\u90e8\u9580, 0, CONVERT(nvarchar(128), \u90e8\u9580ID)<br \/>\nFROM dbo.\u516c\u53f8\u7d44\u7e54 WHERE \u76f4\u5c6c\u90e8\u9580=N&#8217;ROOT&#8217;<\/p>\n<p>UNION ALL<\/p>\n<p>&#8211;UNION ALL\u5f8c\u65b9\u7684\u90e8\u5206\u7a31\u70baRecursive Member, \u6703\u5728\u905e\u8ff4\u904e\u7a0b\u4e2d\u53cd\u8986\u57f7\u884c,<br \/>\n&#8211;\u76f4\u5230\u7121\u4efb\u4f55\u67e5\u8a62\u7d50\u679c\u70ba\u6b62<br \/>\nSELECT P.\u90e8\u9580\u540d\u7a31, P.\u76f4\u5c6c\u90e8\u9580, B.\u5c64\u7d1a+1,<br \/>\nCONVERT(nvarchar(128), B.\u6392\u5e8f\u6b04\u4f4d + &#8216;-&#8216; + CONVERT(nvarchar(128), P.\u90e8\u9580ID))<br \/>\nFROM dbo.\u516c\u53f8\u7d44\u7e54 P, \u516c\u53f8\u7d44\u7e54\u7d50\u679c B<br \/>\nWHERE P.\u76f4\u5c6c\u90e8\u9580=B.\u90e8\u9580\u540d\u7a31<br \/>\n)<br \/>\nSELECT (REPLICATE(&#8216;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8216;, \u5c64\u7d1a) + \u90e8\u9580\u540d\u7a31) as &#8216;\u90e8\u9580\u540d\u7a31&#8217;, \u5c64\u7d1a, \u6392\u5e8f\u6b04\u4f4d<br \/>\nFROM \u516c\u53f8\u7d44\u7e54\u7d50\u679c<br \/>\nORDER BY \u6392\u5e8f\u6b04\u4f4d<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/4.blog.xuite.net\/4\/c\/8\/5\/14458933\/blog_223897\/txt\/30614743\/8.jpg?resize=315%2C275\" alt=\"\" width=\"315\" height=\"275\" border=\"0\" \/><\/p>\n<p>\u53e6\u4e00\u500b CTE \u905e\u8ff4\u7bc4\u4f8b\u00a0(\u53c3\u8003\uff1aJourney to SQL Authority with Pinal Dave)<\/p>\n<p>Code\uff1a<\/p>\n<p>USE AdventureWorks<br \/>\nGO<br \/>\nWITH Emp_CTE AS (<br \/>\n&#8211;\u627e\u51fa ManagerID \u70ba NULL \u7684\u8cc7\u6599\u7576\u4f5c Anchor Member<br \/>\nSELECT EmployeeID, ContactID, LoginID, ManagerID, Title, BirthDate<br \/>\nFROM HumanResources.Employee<br \/>\nWHERE ManagerID IS NULL<\/p>\n<p>UNION ALL<\/p>\n<p>&#8211;\u4e4b\u5f8c\u4ee5 Anchor Member \u70ba\u4f9d\u64da\u905e\u8ff4\u67e5\u8a62<br \/>\nSELECT e.EmployeeID, e.ContactID, e.LoginID, e.ManagerID, e.Title, e.BirthDate<br \/>\nFROM HumanResources.Employee e<br \/>\nINNER JOIN Emp_CTE ecte ON ecte.EmployeeID = e.ManagerID<br \/>\n)<br \/>\nSELECT *<br \/>\nFROM Emp_CTE<br \/>\nGO<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/4.blog.xuite.net\/4\/c\/8\/5\/14458933\/blog_223897\/txt\/30614743\/9.jpg?resize=677%2C296\" alt=\"\" width=\"677\" height=\"296\" border=\"0\" \/><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL &#8211; \u4f7f\u7528 \u4e00\u822c\u8cc7\u6599\u8868\u904b\u7b97\u5f0f CTE (Common Table Expression) \u65e9 &hellip; <a href=\"https:\/\/jason695.why3s.tw\/wordpress\/2011\/08\/24\/%e8%bd%89%e8%b2%bc%e9%97%9c%e6%96%bccte\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587 <span class=\"screen-reader-text\">[\u8f49\u8cbc]\u95dc\u65bcCTE<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[47],"tags":[],"class_list":["post-1898","post","type-post","status-publish","format-standard","hentry","category-sql-server","no-featured-image"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/posts\/1898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/comments?post=1898"}],"version-history":[{"count":0,"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/posts\/1898\/revisions"}],"wp:attachment":[{"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/media?parent=1898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/categories?post=1898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jason695.why3s.tw\/wordpress\/wp-json\/wp\/v2\/tags?post=1898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}