I have a stored procedure in SQL 2014 that makes a dynamic query inside and return two static columns and the rest of columns are dynamic. If I need to put the result of my sp into a temp table, I need to create my temp table specifically with the name of the columns and data types.
How can I just put the result into a temp table without declaring the temp table?
This is how I call it:
SELECT * INTO #TempTable
FROM OPENROWSET
('SQLNCLI','Server=cimplsql-d1.turtle.local;Trusted_Connection=yes;', 'EXEC [dbo].[AttributeSelect] ''2016-01-01'', ''2016-01-01'', 1')
I can't make my temp table well defined because the result is dynamic, it can return 4, 10 or 20 columns, I don't know.
This is the error now:
The metadata could not be determined because statement 'EXEC SP_EXECUTESQL @Qry;' in procedure 'AttributeEntitiesSelect' contains dynamic SQL. Consider using the WITH RESULT SETS clause to explicitly describe the result set.
I used the option WITH RESULT SETS UNDEFINED on my dynamic sql inside the sp, but didn't work