views:

146

answers:

4

In SQL Server, there's no way to create a temp table on the fly from the results of a stored procedure, ala:

CREATE TABLE #temptable AS
EXEC spMyStoredProc

or

EXEC spMyStoredProc INTO #temptable

or something like that. Instead, you have to know the SP layout beforehand, and have to do something like this:

CREATE TABLE #temptable (col1 INT, col2 VARCHAR(255))

INSERT INTO #temptable
EXEC spMyStoredProc

Is there a functional reason why this is the case? Maybe a limitation of SQL Server? Or is it just something that hasn't been added to the SQL spec yet, and I can hold out hope that one day they'll support it?

A: 

Not from a sproc, but you can use tabled values functions to do something similar.

Select * From fnMyFunction

You'd be able to insert into a # table if you desired.

DeletedAccount
I've used table-based UDFs before, but they don't support a number of things that SPs do, like making decisions based on the date. And if an existing system is built with SPs, there's nothing I can do about that. I've just always wondered why SPs don't support the exact thing you're describing with UDFs.
rwmnau
Fair enough. I'll leave the answer tho incase it helps a future googler.
DeletedAccount
I'm not sure what the general consensus is, but I'm almost never in favor of deleting an answer - even if it doesn't help me, it's bound to help somebody in the future. Thanks for your input, especially since it's more a head-scratcher for me than looking for an actual solution, since I don't think one exists.
rwmnau
+9  A: 

A stored procedure can return many result sets, or none, and it can vary entirely depending upon the execution of the stored procedure.

When it is compiled it's meta-information does not describe it as having any specific expectable result set output.

I expect given those constraints, they elected not to implement this because of the lack of strong typing of what a stored procedure may return.

polyglot
I can see why they wouldn't implement this, but there are a number of ways to get the return schema of an SP in VB or C#, so I'd think the same thing would apply here. I suppose an unpredictable result table format is less than ideal, but it still seems like something you should be able to do, since there are many other, less-predictable, things you're allowed to do in T-SQL. Anyway, thanks for your insight!
rwmnau
Cool - no probs, you have to remember though the behaviour and initial implementation goes way back to 1995 and before when Microsoft bought the Sybase source code. Back in the day what you are talking about was a little bit more labour when you are writing it in C rather than a higher level language like .NET or VB!Hence minimalistic restrictions like this! :)
polyglot
A: 

Try this out

DECLARE @temptable TABLE (ID INT, NAME VARCHAR(255))

declare @query varchar(max)

set @query='Select whatever from whereever'

INSERT INTO @temptable

EXEC (@Query)

select *from @temptable
Eric
A: 

If I needed such functionality I would use an inline UDF, like this:

CREATE PROCEDURE MySample
AS
SELECT a,b,c FROM dbo.MyInlineUDF(1,2,3)
GO

SELECT * INTO #t
FROM dbo.MyInlineUDF(1,2,3) WHERE 1=0

INSERT INTO #t EXEC MySample
AlexKuznetsov