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.
Maybe it has something to do with access. If you create a global temp table, it will work.
DECLARE @SQL Varchar(4000)
SET @SQL = 'SELECT * INTO ##tmp_tab FROM dbo.batch'
EXEC(@SQL)
SELECT * FROM ##tmp_tab
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.