SQL SERVER – Select Columns from Stored Procedure Resultset

It is fun to go back to basics often. Here is the one classic question:

“How to select columns from Stored Procedure Resultset?”

Though Stored Procedure has been introduced many years ago, the question about retrieving columns from Stored Procedure is still very popular with beginners. Let us see the solution in quick steps.

First we will create a sample stored procedure.

CREATE PROCEDURE SampleSP
AS
SELECT
1 AS Col1, 2 AS Col2
UNION
SELECT
11, 22
GO

Now we will create a table where we will temporarily store the result set of stored procedures. We will be using INSERT INTO and EXEC command to retrieve the values and insert into temporary table.

CREATE TABLE #TempTable (Col1 INT, Col2 INT)
GO
INSERT INTO #TempTable
EXEC SampleSP
GO

Next we will retrieve our data from stored procedure.

SELECT *
FROM #TempTable
GO

Finally we will clean up all the objects which we have created.

DROP TABLE #TempTable
DROP PROCEDURE SampleSP
GO

Let me know if you want me to share such back to basic tips.

Reference: Pinal Dave (http://blog.SQLAuthority.com)

About these ads

9 thoughts on “SQL SERVER – Select Columns from Stored Procedure Resultset

  1. Other options to avoid #temptable:
    1) If single query in procedure then we can use view instead of procedure & can use it as table in select query.
    2) if multiple operations then we can also use table valued function instead of procedure & can use it as table in select query.

    procedure requires only if we want to do DML operations in our code block…

    Like

  2. Sometimes stored proc returns many columns with a variety of data types. In this case, developer needs to query the result set without creating a temp table because it really becomes a pain. Are there any shorcut solutions for this case?

    Like

  3. for example:
    select a.*
    from
    openrowset(‘SQLOLEDB’,’SERVER=servername;Trusted_Connection=yes;’,
    ‘ set fmtonly off;
    exec dbname.schemaname.procname’) a

    Like

  4. i am new to this field if you can please tell me if it is possible to do this it will be very help full. what i want to do is from my table i want to take first row and try to use it as a column name to create another table by SP. the reason why i want to use those particular column name is which will come in as data in first row is, it will keep changing every week since those are dates. so my plan is if it is possible to extract that first line from data which is ( [Employee Name], [10/18/2013], [10/19/2013]..) and create another table which will use that extracted data as “column name” and create another SP will fill data as required. but i have no clue if SQL will create table and use “column name” which extracted from another data.

    Thanks
    Rav

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s