How do I do a SELECT * INTO [temp table] FROM [stored procedure]
? Not FROM [Table]
and without defining [temp table]
?
Select
all data from BusinessLine
into tmpBusLine
works fine.
select *
into tmpBusLine
from BusinessLine
I am trying the same, but using a stored procedure
that returns data, is not quite the same.
select *
into tmpBusLine
from
exec getBusinessLineHistory '16 Mar 2009'
Output message:
Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword 'exec'.
I have read several examples of creating a temporary table with the same structure as the output stored procedure, which works fine, but it would be nice to not supply any columns.
In SQL Server 2005 you can use
INSERT INTO ... EXEC
to insert the result of a stored procedure into a table. From MSDN'sINSERT
documentation (for SQL Server 2000, in fact):If the query doesn't contain parameter, use
OpenQuery
else useOpenRowset
.Basic thing would be to create schema as per stored procedure and insert into that table. e.g.:
This stored proc does the job:
It's a slight rework of this: Insert stored procedure results into table so that it actually works.
If you want it to work with a temporary table then you will need to use a
##GLOBAL
table and drop it afterwards.I found Passing Arrays/DataTables into Stored Procedures which might give you another idea on how you might go solving your problem.
The link suggests to use an Image type parameter to pass into the stored procedure. Then in the stored procedure, the image is transformed into a table variable containing the original data.
Maybe there is a way this can be used with a temporary table.
If you want to do it without first declaring the temporary table, you could try creating a user-defined function rather than a stored procedure and make that user-defined function return a table. Alternativly, if you want to use the stored procedure, try something like this: