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

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
DATABASE_NAME varchar(128),
-- Insert the output of stored procedures into temp table
INSERT INTO #TEMP EXEC sp_databases;
-- Return Data From temp table 

See all articles by Greg Larsen

Gregory 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.

Latest Articles