For this to work, you need SQL Server 2008.
We use temporary tables and table variables occasionally and sometimes need to fill those temporary objects with data from the results of a stored procedure or dynamically built query (executed with sp_ExecuteSQL). This was a little tricky pre 2008 – as it turns out in SQL Server 2008 this can be done easily.
DECLARE @jobs TABLE
( jobcount int
)
INSERT
INTO @jobs
EXEC
sp_executesql
N’select 1 AS jobcount’
SELECT
*
FROM @jobs
Here, I define a table variable and then insert into this from the execution of a query. I then select from the variable to proove that data was populated. This could easily be a Stored Procedure.
I am sure you can find uses for this , I think this is another good example of the lesser known additions Microsoft has included in 2008.

4 comments
Comments feed for this article
May 20, 2008 at 8:28 pm
Bill Clinton
This is even better than the invention of the cigar.
September 30, 2008 at 3:58 pm
Phaneesh Gururaj
This was very helpful !!
October 13, 2008 at 8:33 am
Igor
It’s not a 2008 feature. it was there since i remember… from sql server 2000? It’s a great feature anyway!
January 24, 2009 at 7:39 am
AdmOd
This is what I want for a week!!
Thanks