I am trying to insert result of dynamic sql into temp table. Important thing is i dont know the column names in advance. As per the SO suggestion the following should work
INSERT into #T1 execute ('execute ' + @SQLString )
also, omit the EXECUTE
if the sql string is something other than a procedure.
However this is not working on SQL 2017
CREATE TABLE Documents(DocumentID INT, Status NVARCHAR(10))
INSERT INTO Documents(DocumentID,Status)
VALUES
(1,'Active'),
(2,'Active'),
(3,'Active'),
(4,'Active')
DECLARE @SQLString NVARCHAR(MAX)
SET @SQLString = 'SELECT * FROM Documents'
INSERT into #T1 execute ('execute ' + @SQLString )
I get error `Invalid object name '#T1'.`
Then i tried by omitting execute
INSERT into #T1 execute (@SQLString)
with same error `Invalid object name '#T1'.`
I should be able to do
SELECT * FROM #T1