Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Slideshows
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

MS SQL

Posted November 3, 2016

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

By Greg Larsen

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



MS SQL Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
SQL 2005: SSIS: Error using SQL Server credentials poverty 3 August 17th, 07:43 AM
Need help changing table contents nkawtg 1 August 17th, 03:02 AM
SQL Server Memory confifuration bhosalenarayan 2 August 14th, 05:33 AM
SQL Server Primary Key and a Unique Key katty.jonh 2 July 25th, 10:36 AM