1) CREATE TABLE #TempTable --local temp table
Local temporary tables are visible only in the current session, and can be shared between nested stored procedure calls: http://www.sommarskog.se/share_data.html#temptables
2) DECLARE TABLE @TempTable --local table variable
The scope of a local variable is the batch, stored procedure, or statement block in which it is declared. They can be passed as parameters between procedures. They are not subject to transactions and will retain all rows following a rollback.
3) SELECT * FROM (SELECT * FROM Customers) AS TempTable --derived table
is visible to the current query only
4) CREATE TABLE ##TempTable --global temp table
This differs from a #temp table in that it is visible to all processes. When the creating process ends, the table is removed (but will wait until any current activity from other processes is done).
5) CTE - common table expression
example CTE:
;WITH YourBigCTE AS
(
big query here
)
SELECT * FROM YourTable1 WHERE ID IN (SELECT ID FROM YourBigCTE)
UNION
SELECT * FROM YourTable2 WHERE ID IN (SELECT ID FROM YourBigCTE)
can be used multiple times within the same CTE command, even recursively, and will last for the duration of the CTE command.