I am trying to write some dynamic SQL queries that select results into a temp table with a query string. It looks like follows:
DECLARE @SQL Varchar(4000)
SET @SQL = 'SELECT * INTO #tmp_tab FROM dbo.sometable'
EXEC(@SQL)
It doesn't give any error to run the code, but when I want to select from #tmp_tab
, it says the table doesn't exist.
So I am wondering if there is any special syntax for it, or dynamic SQL doesn't support such operation?
Many thanks.