Click here to Skip to main content
15,895,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How do I do if want to create table depending on the output of stored procedure.

In this case stored procedure output could be different. It may contain 3 columns , 4 columns with different difference data type.

Is there any way to achieve it with below statement :

SQL
SELECT * Into #MyTable FROM EXEC dbname.dbo.sp_Report @Accountid

SELECT * FROM #MyTable 
Posted

Not using that syntax, you need to define the destination table in advance.

However, you could use OPENROWSET

From stack overflow[^], have a read through the different options

SQL
SELECT * INTO #MyTempTable FROM OPENROWSET('SQLNCLI', 'Server=(local)\SQL2008;Trusted_Connection=yes;',
     'EXEC getBusinessLineHistory')
 
Share this answer
 
Comments
dr.kitanjal 15-Dec-11 8:26am    
But how could I pass @Accountid ?

SELECT * INTO #MyTempTable FROM OPENROWSET('SQLNCLI', 'Server=(local)\SQL2008;Trusted_Connection=yes;',
'EXEC getBusinessLineHistory' + @Accountid )

Result into error
Dylan Morley 15-Dec-11 8:32am    
You can put the syntax into dynamic query string and execute

http://social.msdn.microsoft.com/Forums/en/transactsql/thread/da88f299-7ed4-433e-9475-ff1d0f8bc6c6

Amir Mahfoozi 15-Dec-11 11:17am    
+5
In addition to Dylan Morley which is correct in my opinion, it is better to extract stored procedure content and use it in your insert statement.

In case you preferred OPENROWSET, to run it without error, firstly you should run these two statement :

SQL
sp_configure 'show advanced options', 1
reconfigure
go
sp_configure 'Ad Hoc Distributed Queries', 1 
reconfigure
  go
SELECT * FROM OPENROWSET('SQLNCLI', 
'Server=(local)\SQL2008;Trusted_Connection=yes;',
     'EXEC dbname.dbo.spname parameter')



Good Luck
 
Share this answer
 
Comments
dr.kitanjal 16-Dec-11 1:04am    
But in this case my "EXEC dbname.dbo.spname parameter" is not fixed . It may be any valid SQL query or SP or SP with parameters
Amir Mahfoozi 16-Dec-11 1:12am    
So try to try to implement an algorithm that builds that statement. If it was not run correctly it was another thing. but when this constant phrase executes correctly so the only thing that remains is to make it be some algorithm. Could you run a sample query correctly so far by this way?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900