How to execute a stored procedure and save the results in a temp table for an unknown schema

I wanted to execute a stored procedure and save its results in a temp table. I do not know what data will be returned by this stored procedure, so I prefer to create a temporary table on the fly.

This is a bunch of code that I used after surfing the sites to get a way to do this:

sp_configure 'Show Advanced Options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO

SELECT * INTO #TestTableT FROM OPENROWSET('SQLNCLI', 'Server=localhost;Trusted_Connection=yes;',
'EXEC [dbo].[aaaaaaa]')
-- Select Table
SELECT *
FROM #TestTableT;

The problem I am facing is that after executing the above code, I get

Msg 11529, Level 16, State 1, Procedure sp_describe_first_result_set, Line 1

Metadata cannot be determined because each code path results in an error; see previous errors for some of them.

Msg 2812, 16, 62, sp_describe_first_result_set, 1 "dbo.aaaaaaa".

+4
1

, my be you badpell table name

sp_configure 'Show Advanced Options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO

SELECT * INTO #TestTableT FROM OPENROWSET('SQLNCLI', 'Server=localhost;Trusted_Connection=yes;',
'EXEC YourdbName.[dbo].[aaaaaaa]')
-- Select Table
SELECT *
FROM #TestTableT;
0

All Articles