Search This Blog

Thursday, March 21, 2013

Insert values and create a temporary table from a dynamic query SQL Server

SQL Server > Scripts > Insert dynamic temp table

Global temporary tables are visible to any user and any connection after they are created, and are deleted when all users that are referencing the table disconnect from the instance of SQL Server

DECLARE @sql varchar(max)
IF object_id('tempdb.dbo.##MyTemp') Is Not Null
       DROP TABLE ##MyTemp
SET @sql =
'SELECT * INTO ##MyTemp FROM (select name from sys.all_objects ) as tbl'
Exec (@sql)
go
SELECT * FROM ##MyTemp