Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 select output of sp into undefined temp table

Author  Topic 

subt13
Starting Member

11 Posts

Posted - 2010-01-05 : 19:41:50
Hello, I have a dynamic query that I'm executing with sp_executesql and I want to take its output and insert it into a #temptable that will be defined by the output of the sp.

pseudo code:

insert exec sp_executesql @theDynamicSql into #temptable.

The reason the #temp table must be defined by the sp output is that the number of columns and the names of columns will change.

Thanks.

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-01-06 : 01:45:36
use select into
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-01-06 : 02:08:49
Use method 2
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/26/select-columns-from-exec-procedure-name-is-this-possible.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

subt13
Starting Member

11 Posts

Posted - 2010-01-06 : 11:19:28
openrowset takes a string as the query to run, so can it handle sp_executesql @param in the string?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-06 : 11:24:51
http://support.microsoft.com/kb/314520
Go to Top of Page

subt13
Starting Member

11 Posts

Posted - 2010-01-06 : 11:42:44
Also, as an alternative, is it possible to do something like:
select * from ( exec sp_executesql @theDynamicSql ) ?
Go to Top of Page

subt13
Starting Member

11 Posts

Posted - 2010-01-06 : 11:46:37
[code]http://support.microsoft.com/kb/314520[/code] If you read my first post, you will understand that this will run into the same problem with exec. Reread my first post.

Thanks.
Go to Top of Page

subt13
Starting Member

11 Posts

Posted - 2010-01-06 : 12:29:36
I have found a solution that will work. I will just create the temp table in the dynamic query using ## instead.
Go to Top of Page
   

- Advertisement -