{"id":1520,"date":"2013-03-08T10:58:13","date_gmt":"2013-03-08T10:58:13","guid":{"rendered":"http:\/\/galhano.com\/blog\/?p=1520"},"modified":"2013-03-08T11:41:27","modified_gmt":"2013-03-08T11:41:27","slug":"recursive-queries-using-common-table-expressions-cte-in-sql-server","status":"publish","type":"post","link":"http:\/\/galhano.com\/blog\/?p=1520","title":{"rendered":"Recursive Queries using Common Table Expressions (CTE) in SQL Server"},"content":{"rendered":"<p>Given the example , hierarchical data structures, organizational charts and other parent-child table relationship reports can easily benefit from the use of recursive CTEs. Common Table Expression is just one of those T-SQL enhancements available for SQL Server 2005. CTEs bring us the chance to create much more complex queries while retaining a much simpler syntax. They also can lessen the administrative burden of creating and testing views for situations where the view will not be reused.<\/p>\n<p>Syntax<\/p>\n<p>[sql]<br \/>\nWITH cte_alias (column_aliases)<br \/>\nAS<br \/>\n(<br \/>\ncte_query_definition   &#8211;initialization<br \/>\nUNION ALL<br \/>\ncte_query_definition2 &#8211;recursive execution<br \/>\n)<br \/>\nSELECT * FROM cte_alias<br \/>\n[\/sql]<\/pre>\n<p>Sample (from Root nodes to Leaf\u00a0 noes)<\/p>\n<p>[sql]<br \/>\nWITH Managers AS<br \/>\n(<br \/>\n&#8211;initialization<br \/>\nSELECT EmployeeID, LastName, ReportsTo<br \/>\nFROM Employees<br \/>\nWHERE ReportsTo IS NULL<br \/>\nUNION ALL<br \/>\n&#8211;recursive execution<br \/>\nSELECT e.employeeID,e.LastName, e.ReportsTo<br \/>\nFROM Employees e INNER JOIN Managers m<br \/>\nON e.ReportsTo = m.employeeID<br \/>\n)<br \/>\nSELECT * FROM Managers<br \/>\n[\/sql]<\/pre>\n<p>Sample (From\u00a0Leaf nodes to Root nodes), where u must specifie the leaves to include\/use.<\/p>\n<p>[sql]<br \/>\nWITH xEmployees AS<br \/>\n(<br \/>\n&#8211;initialization<br \/>\nSELECT\u00a0 EmployeeID, LastName, ReportsTo<br \/>\nFROM Employees<br \/>\nWHERE EmployeeID IN (55,98,65,12)\u00a0 &#8212; sample Leaf nodes to filter\/use<br \/>\nUNION ALL<br \/>\n&#8211;recursive execution<br \/>\nSELECT a.EmployeeID,a.LastName,a.ReportsTo<br \/>\nFROM Employees a INNER JOIN xEmployees m<br \/>\nON a.EmployeeID = m.reportsTo<br \/>\n)<br \/>\nSELECT Distinct EmployeeID, LastName, ReportsTo\u00a0 FROM xEmployees<br \/>\n[\/sql]<\/p>\n<p>!Importante to use <span style=\"color: #800000;\">&#8220;Distinct<\/span>&#8221; statement!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given the example , hierarchical data structures, organizational charts and other parent-child table relationship reports can easily benefit from the use of recursive CTEs. Common Table Expression is just one of those T-SQL enhancements available for SQL Server 2005. CTEs bring us the chance to create much more complex queries while retaining a much simpler [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"close","ping_status":"close","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40,9,104],"tags":[],"class_list":["post-1520","post","type-post","status-publish","format-standard","hentry","category-sql","category-sql-server","category-transact-sql","author-admin"],"_links":{"self":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1520"}],"version-history":[{"count":6,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1520\/revisions"}],"predecessor-version":[{"id":1523,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1520\/revisions\/1523"}],"wp:attachment":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1520"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}