Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ul> <li><p>In <code>SQL Server 2008</code> (and in <code>Oracle 10g</code>): <a href="http://technet.microsoft.com/en-us/library/bb510625.aspx" rel="nofollow noreferrer"><code>MERGE</code></a>.</p> <p>A single command to <code>INSERT / UPDATE / DELETE</code> into a table from a row source.</p></li> <li><p>To generate a list of numbers from <code>1</code> to <code>31</code> (say, for a calendary):</p> <pre><code>WITH cal AS ( SELECT 1 AS day UNION ALL SELECT day + 1 FROM cal WHERE day &lt;= 30 ) </code></pre></li> <li><p>A single-column index with <code>DESC</code> clause in a clustered table can be used for sorting on <code>column DESC, cluster_key ASC</code>:</p> <pre><code>CREATE INDEX ix_column_desc ON mytable (column DESC) SELECT TOP 10 * FROM mytable ORDER BY column DESC, pk -- Uses the index SELECT TOP 10 * FROM mytable ORDER BY column, pk -- Doesn't use the index </code></pre></li> <li><p><code>CROSS APPLY</code> and <code>OUTER APPLY</code>: enables to join rowsources which depend on the values of the tables being joined:</p> <pre><code>SELECT * FROM mytable CROSS APPLY my_tvf(mytable.column1) tvf SELECT * FROM mytable CROSS APPLY ( SELECT TOP 5 * FROM othertable WHERE othertable.column2 = mytable.column1 ) q </code></pre></li> <li><p><code>EXCEPT</code> and <code>INTERSECT</code> operators: allow selecting conditions that include <code>NULL</code>s</p> <pre><code>DECLARE @var1 INT DECLARE @var2 INT DECLARE @var3 INT SET @var1 = 1 SET @var2 = NULL SET @var2 = NULL SELECT col1, col2, col3 FROM mytable INTERSECT SELECT @val1, @val2, @val3 -- selects rows with `col1 = 1`, `col2 IS NULL` and `col3 IS NULL` SELECT col1, col2, col3 FROM mytable EXCEPT SELECT @val1, @val2, @val3 -- selects all other rows </code></pre></li> <li><p><code>WITH ROLLUP</code> clause: selects a grand total for all grouped rows</p> <pre><code>SELECT month, SUM(sale) FROM mytable GROUP BY month WITH ROLLUP Month SUM(sale) --- --- Jan 10,000 Feb 20,000 Mar 30,000 NULL 60,000 -- a total due to `WITH ROLLUP` </code></pre></li> </ul>
 

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