Using a temporary table in dynamic sql in a stored procedure

只谈情不闲聊 提交于 2019-12-11 01:29:08

问题


I am writing a Store Procedure in SQL Server 2012. I have a temporary table defined like so:

  DECLARE @CURRENT_RET_WEEK_PTIMEIDS TABLE ( PTIMEID INT )

I am also using EXECUTE to write a dynamic SQL query. Is there any way I can join this table onto the above temporary table?


回答1:


Try to use local temp-table -

IF OBJECT_ID ('tempdb.dbo.#temp') IS NOT NULL
   DROP TABLE #temp

CREATE TABLE #temp (ID INT)
INSERT INTO #temp (ID)
VALUES (1),(2)

DECLARE @SQL NVARCHAR(MAX)
SELECT @SQL = 'SELECT * FROM #temp'

EXEC sys.sp_executesql @SQL


来源:https://stackoverflow.com/questions/18505449/using-a-temporary-table-in-dynamic-sql-in-a-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!