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 2005 Forums
 Transact-SQL (2005)
 How to exec SP into tmp table w/only certain colum

Author  Topic 

boo2
Starting Member

5 Posts

Posted - 2009-08-21 : 23:11:32
I need to execute a SP and put the result into a temp table; the SP returns 43 columns and I only need 3. I don't want to create a temp table with 40 columns I don't need, is there a way to do this:

INSERT INTO #tmpResults (COL1, COL2, COL3) EXEC (@param = val1, etc.)

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-08-21 : 23:47:48
No. the columns in the temp table must match the result of the stored procedure


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2009-08-21 : 23:55:34
You can use the openrowset to do what you want.

http://blogs.technet.com/wardpond/archive/2005/08/01/408502.aspx


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

boo2
Starting Member

5 Posts

Posted - 2009-08-22 : 00:09:38
That's way beyond my skillset (openrowset). Can you provide an example of just calling a (same database) stored procedure and just selecting a couple of columns from that?

Creating a temptable with 43 columns just seems non-pragmatic
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2009-08-22 : 00:46:07

Select *
from OPENROWSET('SQLOLEDB','Data Source=**TYPEYOURSERVER/INSTANCE**;Trusted_Connection=yes;Database=**TYPEYOUR DATABASENAME**;Integrated Security=SSPI', 'EXEC MYSTOREDPROCEDURE')

So if your server was called server1 and you were running sqlexpress and you had a database named database1 and a stored procedure called mystoredprocedure and you wanted the column names col1,col2 and col3 returned.



Select COL1,Col2,Col3
from OPENROWSET('SQLOLEDB','Data source=server1/SQLEXPRESS;Trusted_Connection=yes;Database=database1;Integrated Security=SSPI', 'EXEC MYSTOREDPROCEDURE')





Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page
   

- Advertisement -