Insert Stored Procedure results into a table

Basically we have 2 options to push Stored Procedure results.

1. If you do not know the schema and you may need to generate the schema during run time.

-- Change your instance Name 
SELECT * INTO #sp_whoResults FROM OPENROWSET('SQLNCLI', 'Server=localhost\mssqlserver2014;Trusted_Connection=yes;', 'EXEC sp_who')
GO

select * from #sp_whoResults

NOTE: You may not be able to use above solution in all scenarios (Ex: SP_Who2)

2. If you know the schema, and if the table already exists, you can use following method to add records into the table.

truncate table #sp_whoResults
GO

insert into #sp_whoresults
EXEC sp_who
GO

select * from #sp_whoResults
Advertisements
This entry was posted in Productivity, Script, SQL, SQL Query and tagged , , . Bookmark the permalink.

One Response to Insert Stored Procedure results into a table

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