簡體   English   中英

SQL exec 語句后的 object 名稱無效

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM