Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>@table</code> syntax creates a table variable (an actual table in <code>tempdb</code>) and materialises the results to it. </p> <p>The <code>WITH</code> syntax defines a <a href="http://msdn.microsoft.com/en-us/library/ms190766.aspx" rel="nofollow noreferrer">Common Table Expression</a> which is not materialised and is just an inline View.</p> <p>Most of the time you would be better off using the second option. You mention that this is inside a function. If this is a TVF then most of the time you want these to be inline rather than multi statement so they can be expanded out by the optimiser - this would instantly disallow the use of table variables.</p> <p>Sometimes however (say the underlying query is expensive and you want to avoid it being executed multiple times) you might determine that materializing the intermediate results improves performance in some specific cases. There is <a href="http://connect.microsoft.com/SQLServer/feedback/details/218968/provide-a-hint-to-force-intermediate-materialization-of-ctes-or-derived-tables" rel="nofollow noreferrer">currently no way</a> of forcing this for CTEs (<a href="http://explainextended.com/2009/05/28/generating-xml-in-subqueries/" rel="nofollow noreferrer">without forcing a plan guide at least</a>)</p> <p>In that eventuality you (in general) have 3 options. A <code>@tablevariable</code>, <code>#localtemp</code> table and a <code>##globaltemp</code> table. However only the first of these is permitted for use inside a function.</p> <p>For further information regarding the differences between table variables and #temp tables <a href="https://dba.stackexchange.com/questions/16385/whats-the-difference-between-a-temp-table-and-table-variable-in-sql-server/16386#16386">see here</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload