SQL - How to make table result available to the calling script

tommym121
tommym121 used Ask the Experts™
on
I have to execut this statement, it will return result as a table.  How can I make use of it
EXEC ('select ' + @columnsList + ',  '+ @SQLValuesSetTemplate + 'as ValueSet From ' + @sourceTable)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Do you want to use output of execution call ??..

You can create a temp table with structure similar to output result.


insert into #temp_table
EXEC ('select ' + @columnsList + ',  '+ @SQLValuesSetTemplate + 'as ValueSet From ' + @sourceTable)


select * from #temp_table

Author

Commented:
saurv

I try, I can not get it working. Would you be able to look at it what is missing?


DECLARE @str varchar(max)
Set @str=
'Select * from T1'
insert into #tempTable123
Exec (@str)
Print 'Hello' ;

Select * from #tempTable123

I got this error
Msg 208, Level 16, State 0, Line 5
Invalid object name '#tempTable123'.

Author

Commented:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial