开发者

retrieve a specific column from a stored proc result set into a temp table

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜