[英]SQL Invalid object name after exec statement
這是代碼
declare @test nvarchar(1000)
set @test = 'select * into #temp from mytable'
execute sp_executesql @test
select * from #temp
然后它返回:無效的 object 名稱 '#temp'。
我試圖從mytable
創建一個臨時表temp
。 但是,似乎#temp
不是在execute
之后創建的。 但是,如果我into #temp
,它確實有效。
本地臨時表 ( #tmp
) 在當前 session 中可用,這意味着sp_executesql
在另一個 session 中執行。 要訪問臨時表,您應該使用兩個 # ( ##tmp
) 定義全局臨時表,然后您的代碼將以這種方式工作:
declare @test nvarchar(1000)
set @test = 'select * into ##temp from mytable'
execute sp_executesql @test
select * from ##temp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.