How to Get the Output of a Stored Procedure into a Table | Database Journal

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

Written By
Gregory Larsen
Gregory Larsen
Nov 3, 2016
1 minute read

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

Gregory Larsen

Gregory A. Larsen is a DBA at Washington State Department of Health (DOH). Greg is responsible for maintaining SQL Server and other database management software. Greg works with customers and developers to design and implement database changes, and solve database/application related problems. Greg builds homegrown solutions to simplify and streamline common database management tasks, such as capacity management.

Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.