views:

84

answers:

3

What I need to do is, put the results of stored procedure (with lots of parameters) in to a table. Something like that:

SELECT * INTO Table_1 FROM
(
EXEC [MY_DataBase].[dbo].[GET_Report] '%%', '%%', '%%', 'x', 'x', 'x', 'x', 'x', 'x' ....
) X

However this seems to be an incorrect syntax, I searched for it and people are first creating the table manually and after using the INSERT key to put the results. This is something that I can't do, since I ve got lots of parameters and I can't create the table manually.

+1  A: 

You can do it if you change stored procedure to user defined function.

Giorgi
+4  A: 

You can't use a stored procedure within a SELECT statement like this, meaning you can't create the target table inline at the point of execution using the sproc.

You either need to:

1) create Table_1 first with the correct schema and then do:
INSERT Table_1 (fieldlist) EXECUTE YourSproc....

2) rewrite the sproc as a unser defined function and use that in the SELECT INTO statement:

SELECT *
INTO Table_1
FROM dbo.YourFunction(same params as sproc...) x
AdaTheDev
I guess your second argument is not correct or is working with higher versions of SQL Server, I am using SQL Server 2000 and, this one worked for me. SELECT * INTO Table_1 FROM (SELECT * FROM dbo.YourFunction(same params as sproc...)) xThanks for helps.
stckvrflw
Yes, in SQL 2000 you can't use a UDF in a SELECT INTO statement. Does work from SQL 2005 onwards.
AdaTheDev
A: 

You can try this solution, it involves: enabling ad hoc queries, using SELECT..INTO to create a temp table and using OPENROWSET

-- you will need to enable Ad Hoc Remote Queries
EXEC sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE 
GO

-- this assumes trusted connection
SELECT tmp.*
INTO #mytemptable
FROM OPENROWSET ('SQLOLEDB',
     'Server=SQLVM\SQL01;TRUSTED_CONNECTION=YES;Database=pubs',
     'EXEC sp_who') tmp

-- select from your temporary table
SELECT *
FROM #mytemptable
sqlbelle