Welcome to TechNet Blogs Sign in | Join | Help

The OPENROWSET Trick: Accessing Stored Procedure Output In A SELECT Statement

Updated 20 Mar 2009: This code is revisited here.

This nifty little trick will allow you, with some limitations, to treat the output of a stored procedure as a named SQL record set rather than creating a table and going through an INSERT.. EXEC process.  The output of the stored procedure is then available for direct manipulation in SELECT statements, JOINs, etc.

I’ve passed on this nugget many times since it was first shared with me about five years ago.  I’ve tried to find the person who came up with it first, I’ve lately heard from someone who published it in a newsgroup back in 1999.  A shy individual, this person wants no part of my efforts to publicly recognize the genius behind this approach (my favorite comment from a colleague, upon seeing this, was “That's sick!  In a twisted, useful, and instructive way, of course!”)

This syntax works in both SQL Server 2000 and SQL Server 2005, and requires integrated security to be turned on.  Under SQL Server 2005, Ad Hoc Distributed Queries must be enabled.

Here’s a simple sample that assigns the output from master.sp_who to a derived table called tbl:

SELECT  *
FROM    OPENROWSET ('SQLOLEDB','Server=(local);TRUSTED_CONNECTION=YES;','set fmtonly off exec master.dbo.sp_who')
AS tbl

Here’s a slightly more complex (but perhaps ultimately silly) example that joins the output from two stored procedures:

SELECT  who.loginame AS LoginName,
        who.HostName, 
        DB_NAME(locks.dbid) AS DatabaseName, 
        locks.Type
FROM    OPENROWSET ('SQLOLEDB','Server=(local);TRUSTED_CONNECTION=YES;','set fmtonly off exec master.dbo.sp_who')
AS  who
JOIN    OPENROWSET ('SQLOLEDB','Server=(local);TRUSTED_CONNECTION=YES;','set fmtonly off exec master.dbo.sp_lock')
AS  locks
ON  who.spid = locks.spid

A couple of notes:

  • The ‘set fmtonly off’ is included only for completeness.  If you’re certain the FMTONLY will always be set to OFF, it’s safe to omit this (if FMTONLY is set ON, the calls will produce only metadata, no results).
  • The OPENROWSET call opens a separate connection to SQL Server, so there is some overhead associated with this approach.  For this reason, I’d avoid using this technique in an OLTP system as it’s unlikely to scale well (don’t run with scissors).
  • Using this technique with a poorly architected stored procedure could lead to blocking issues.
  • This technique is not supported inside a declared transaction.
Published Monday, August 01, 2005 5:53 PM by Ward Pond

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Loading An XML Document Into SQL Server: Another OPENROWSET Trick

Sunday, April 16, 2006 2:57 AM by Ward Pond's SQL Server blog
A quick-and-dirty solution for importing file-based XML documents into SQL Server tables is discussed.

# A Shiny New Corner of The Blogosphere

Thursday, August 03, 2006 12:50 AM by Ward Pond's SQL Server blog
All I did was ask a question, honest..
When I was vetting my recent post regarding database design issues,...

# http://sqlservercode.blogspot.com/2006/08/store-output-of-stored-procedure-in.html

Tuesday, August 08, 2006 4:08 PM by TrackBack

# http://sqlservercentral.com/forums/shwmessage.aspx?forumid=145&messageid=291512

Thursday, August 31, 2006 1:33 PM by TrackBack

# re: The OPENROWSET Trick: Accessing Stored Procedure Output In A SELECT Statement

Friday, April 11, 2008 1:44 PM by RON

Thank you so much for sharing this.  I've been looking for this solution for a long time.  I really appriceiate it.

# http://eggheadcafe.com/community/aspnet/13/10034706/using-openrowset.aspx

Tuesday, May 06, 2008 9:45 AM by TrackBack

# http://eggheadcafe.com/community/aspnet/9/10032061/openrowset-option.aspx

Monday, September 01, 2008 11:05 PM by TrackBack

# http://eggheadcafe.com/community/aspnet/13/10034703/use-openrowset.aspx

Tuesday, September 23, 2008 7:07 PM by TrackBack

# http://stackoverflow.com/questions/209383/select-columns-from-result-set-of-stored-procedure

Tuesday, November 11, 2008 10:38 AM by TrackBack

# re: The OPENROWSET Trick: Accessing Stored Procedure Output In A SELECT Statement

Wednesday, March 18, 2009 8:30 AM by Giles Collingwood

Nice - does it work with parameters too?

# Database Programming: The OPENROWSET Trick, Revisited

Friday, March 20, 2009 12:08 PM by Ward Pond's SQL Server blog

One of the most popular posts in the history of this little corner of the Internets is one from August,

# http://sqlteam.com/forums/topic.asp?topic_id=70626&whichpage=2

Monday, August 31, 2009 4:42 PM by TrackBack

# http://dbforums.com/microsoft-sql-server/1647218-processing-result-table-sp-select.html

Monday, September 28, 2009 8:37 PM by TrackBack

# http://sqlteam.com/forums/topic.asp?topic_id=131600

Monday, September 28, 2009 8:37 PM by TrackBack

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker