dynamicsql

TSQL Writing into a Temporary Table from Dynamic SQL

Consider the following code: SET @SQL1 = 'SELECT * INTO #temp WHERE ...' exec(@SQL1) SELECT * from #temp (this line throws an error that #temp doesn't exist) Apparently this is because the exec command spins off a separate session and #temp is local to that session. I can use a global temporary table ##temp, but then I have to come ...

Dynamically create temp table, insert into temp table, and then select

Basically i want to be able to dynamically create a temp table based off of an existing table, and then insert values into the temp table, and select the inserted values. i've got the part where i can create the temp table working just fine, it's just that inserting and selecting form it aren't working too well. here's my current code....