regarding Common table expression CTEhttp://forums.asp.net/t/1757803.aspx/1?regarding+Common+table+expression+CTETue, 10 Jan 2012 14:36:01 -050017578034775209http://forums.asp.net/p/1757803/4775209.aspx/1?regarding+Common+table+expression+CTEregarding Common table expression CTE <p>what is CTE and what is the advantage. is there any advantage regarding performance of using CTE. it looks it is like temporary view.</p> <p>WITH MyData AS<br> (<br> select Specialist,jid from bbajobs <br> )<br> <br> select * from MyData</p> <p>please discuss about CTE in detail</p> 2012-01-10T12:51:00-05:004775326http://forums.asp.net/p/1757803/4775326.aspx/1?Re+regarding+Common+table+expression+CTERe: regarding Common table expression CTE <p>Hello mou_inn,</p> <p>For me the most outstandind advantage is you can query hierarchical structures in SQL using CTE expressions. Developers can build <a href="http://www.kodyaz.com/t-sql/sql-server-recursive-query-with-recursive-cte.aspx" target="_blank"> recursive sql queries using CTE in SQL Server</a>&nbsp;. Please check the reference for an example</p> <p>Also you do not need to define temporary tables. You can <a href="http://www.kodyaz.com/articles/sql-multiple-cte-syntax-sql-cte-example-in-sql-server-2008.aspx" target="_blank"> create multiple CTE's</a> in a SQL query and refer to a CTE within the same query&nbsp;more than once.</p> <p>I'm not sure it has a disadvantage when compared with creating temp tables or using sub-selects</p> 2012-01-10T13:59:23-05:004775390http://forums.asp.net/p/1757803/4775390.aspx/1?Re+regarding+Common+table+expression+CTERe: regarding Common table expression CTE <p>You are correct a CTE is like a temporary view. However a CTE is often smaller than a view so they are better to use in a stored procedure rather than creating a proper view for a small set of data. As for performance, that can be <a href="http://www.sqlservercentral.com/Forums/Topic415829-338-1.aspx">debated</a> as to what is really faster but I have never had a performance issue with them.</p> <p>I like using CTEs mainly because they are easy to write and maintain. Also when other developers on my team write sprocs using CTEs they are pretty straight forward to read which helps when debugging something new I did not write myself (or I wrote months or years ago and have forgotten all about). Another good point is that they are fairly easy to use recursion with, which can be quite helpful from time to time. This <a href="http://www.4guysfromrolla.com/webtech/071906-1.shtml">link</a> is a bit older but still relevant and gives a good overview of CTEs.</p> 2012-01-10T14:36:01-05:00