I'm writing a stored procedure. I have a string which contains an sql query. For example:
DECLARE @sql nvarchar(max)
SET @sql = (N'SELECT pkOrderID FROM Orders')
(Just to note: this isn't what the select statement looks like. This is just an example of what I mean) I then want to execute the string and put the result in a temporary table E.g. #tempTable. I know EXEC(@sql) exists but not sure if it will do me any good in this situation. The other twist is that I do not know the names of all the columns in the returned @sql so the temp table #tempTable needs to be created dyanmically based off the return from @sql. Thanks for any help.