How to Get the Output of a Stored Procedure into a Table

Thursday Nov 3rd 2016 by Greg Larsen
Share:

Putting the output of a stored procedure into a table provides you multiple options for parsing and using the output with other TSQL commands. Read on to learn more.

There are times when you might what to execute a stored procedure where you want the output of the stored procedure to go into a table, instead of having the output display across the screen. Putting the output in a table provides you multiple options for parsing and using the output with other TSQL commands. In this tip I will show you an easy way to get the output of a stored procedure into a table.

To get the output of your stored procedure into a table you use the INSERT statement. To do this, first create a table that will hold the output of the stored procedure. In that table create a column for every column that is outputted from your stored procedure. Next execute an INSERT statement where you stream the output of your stored procedure into that INSERT statement. Below is an example of how to do this. In this example I put the output of the sp_databases system stored procedure into a temporary table and then displayed the data in the temporary table using a SELECT statement:

-- Create temporary table to hold output of stored procedure
CREATE TABLE #TEMP (
DATABASE_NAME varchar(128),
DATABASE_SIZE INT, 
REMARKS VARCHAR(400));
 
-- Insert the output of stored procedures into temp table
INSERT INTO #TEMP EXEC sp_databases;
 
-- Return Data From temp table 
SELECT * FROM #TEMP;
 
-- CLEAN UP
DROP TABLE #TEMP;

See all articles by Greg Larsen

Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved