0

I have a stored proc which returns a result set with somewhere around 20 columns. I'm only interested in retrieving all rows of a specific column (username).

Here is what I have:

--create temp table
CREATE TABLE #USERNAMES(
username         char(10)
)

--store results in a temp table
INSERT INTO #USERNAMES
exec dbo.getAccountInfo @subbed = 1

This won't work seeing as it wants to store the entire result set into the temp table, but the temp table has not defined all of the columns it needs. How can I modify the insert to only insert the username column from the getAccountInfo result set into the temp table #USERNAMES?

Yuck
  • 49,664
  • 13
  • 105
  • 135
user234702
  • 229
  • 6
  • 17

1 Answers1

2

If you create a loopback linked server (see my answer here: Retrieve column definition for stored procedure result set) you can use OPENQUERY, e.g.

INSERT #USERNAMES(username)
  SELECT username 
  FROM OPENQUERY(loopback, 'EXEC dbname.dbo.getAccountInfo @subbed = 1;');

NOTE: dbname is important here!

Of course you could always replicate the stored procedure, or give it an optional argument that defines the shape of the resultset.

Community
  • 1
  • 1
Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
  • I'm unable create a loopback linked server. However, I just created an alternate result set only including the column I wanted when you pass in an optional parameter indicator to the proc as you suggested. Thanks. – user234702 Sep 14 '11 at 17:35