Skip to content
 

Insert Results of Stored Procedure Into Table

Retrieving the result set from a stored procedure has historically proven to be a difficult task. Fortunately with the advent of OPENROWSET, our life as developers has become much simpler. Using openrowset is very simple when we do not have procedures that take parameters, However when parameters become involved the game changes, however there is a workaround.

In using OPENROWSET we need to turn on the ability to run distributed queries on your server. To enable this is simple, given you have the appropriate permissions. It’s a simple process when we do not have to pass in parameters.

sp_configure 'Ad Hoc Distributed Queries',1
RECONFIGURE WITH OVERRIDE

Using OPENROWSET for a Stored Procedure with No Parameters

To get the result set is simple, simply call OPENROWSET passing the parameter of driver name, connection string and command text and we’re finished.

SELECT *
INTO #Jobs
FROM OPENROWSET('SQLNCLI', 'server=SERVERNAME\INSTANCENAME;trusted_connection=yes',
'set fmtonly off exec msdb.dbo.sp_help_job')

Using OPENROWSET for a Stored Procedure with Parameters

A whole different scenario presents itself when we need to use the above method to call a stored procedure and pass dynamic parameters. The issue is that you cannot concatenate a string within OPENROWSET. Here is how we do it:

First we need to create a procedure that wraps OPENROWSET. Make sure to replace the server and instance with your servername\instancename values.

CREATE PROCEDURE ProcToTable
(
	@TempTableName nvarchar(100)
	,@SPAndParameters nvarchar(max)
)
AS
BEGIN
	DECLARE @Driver nvarchar(20)
	DECLARE @ConnectionString nvarchar(200)
	DECLARE @SQL nvarchar(max)
	DECLARE @RowsetSQL nvarchar(max)

	SET @Driver = '''' + 'SQLNCLI' + ''''
	SET @ConnectionString = '''' +'server=SERVERNAME\INSTANCENAME;trusted_connection=yes' + ''''
	SET @RowsetSQL = '''' +'SET FMTONLY OFF ' + '' + @SPAndParameters + '' + ''''
	SET @SQL = 'INSERT INTO ##' + @TempTableName + ' SELECT * FROM OPENROWSET('

	SET @SQL = @SQL + @Driver + ',' + @ConnectionString + ',' + @RowsetSQL + ')'
	EXEC (@SQL)
END

-- Now that the procedure is created, let's run our query
SELECT TOP 0 *
INTO #holdingtable
FROM OPENROWSET('SQLNCLI','server=SERVERNAME\INSTANCENAME;trusted_connection=yes','SET FMTONLY OFF exec sp_who 1')
-- set the parameters of the procedure
SET @SPID = 1
SET @SPWithParams = 'exec sp_who ' + @SPID
-- execute the procedure
exec ProcToTable 'holdingtable', @SPWithParams
-- view the results
SELECT * FROM #holdingtable

This procedure we created, uses a temp table to insert the result of our parameterized stored procedure query. So with that in mind, we need to first create the temp table prior to executing the procedure. We need to execute the procedure using default parameters and using a SELECT TOP 0 *, in order to create the structure of our table.

USING OPENQUERY

The other option is to use OPENQUERY. It is basically the same as OPENROWSET, however you can use this option if for some reason you can’t get OPENROWSET to work. There is an additional setup using this. You have to create a linked server connection to your own local server. Click here for instructions to create a local linked server

-- Enable Ad Hoc Distributed Queries
EXEC sp_configure 'Ad Hoc Distributed Queries', 1
RECONFIGURE
GO

SELECT *
INTO dbo.List
FROM OPENQUERY(me, 'set fmtonly off exec dbo.GetList')
ORDER BY 1

Manually creating a Temp table

This last way works fine if you previously know the datatypes and columns that are being returned by the procedure.

CREATE PROCEDURE GetList
AS
BEGIN
SELECT
ListName = 'MyList'
,ListNumber = 1
END
GO

CREATE TABLE #List
(
ListName varchar(25),
ListNumber int
)

INSERT INTO #List
(
ListName,
ListNumber
)
EXEC dbo.GetList
Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

Related Posts:

  • » SQL Server Add Primary Key
  • » SQL Server Select
  • » Compare Stored Procedure Dataset Output
  • » Get End of Month
  • » Beginning of Month
    blog comments powered by Disqus