#TempTables 的范围仅限于 EXEC 语句?
通过尝试这个,
use master
go
select * into #TempTable from sys.all_views
select * from #TempTable
drop table #TempTable
exec('
select * into #TempTable2 from sys.all_views
')
/* This will give error: */
select * from #TempTable2
我意识到 #TempTable2 不可访问...因此在 EXEC 语句中使用 select into #TempTable
语法意味着该表会在 exec 语句完成时自动销毁?
By trying this
use master
go
select * into #TempTable from sys.all_views
select * from #TempTable
drop table #TempTable
exec('
select * into #TempTable2 from sys.all_views
')
/* This will give error: */
select * from #TempTable2
I realized that #TempTable2 is not accessible... so using the select into #TempTable
syntax is used inside an EXEC statement means the table is auto destroyed as the exec statement is completed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的。
来查看它
如果您希望稍后能够访问它,您可以使用
##global
临时表 。例如Yes.
You can see this with
You can use a
##global
temporary table if you want to be able to access it later. e.g.您可以创建一个全局临时表
(如果您这样做,并且您的代码可能由多个用户使用,则在临时表中包含 SPID 列,在选择列表中包含 @@SPID,并将最终选择更改为按SPID=@@SPID)
You could create a global temp table
(If you do this, and your code might be used by multiple users, then include a SPID column in the temp table, include @@SPID in the select list, and change the final select to filter by SPID = @@SPID)
是的,你是对的。
Exec
语句中的临时表只能在该语句内访问。如果您在 SSMS 中打开两个窗口并在一个窗口中创建临时表,您将无法通过另一个窗口访问它,因为它是不同的连接。
但是,如果您创建全局临时表,您将能够访问它。全局临时表是用双 ## 定义的,而不是一个。
SQLTeam 有一篇关于临时表的文章这里,还有来自 MSDN
Yes you are correct. The temp table within the
Exec
statement is only accessible within that statement.If you open two windows in SSMS and create a temp table in one window you won't be able to access it through the other window as it is a different connection.
However, you will be able to access it if you create a global temp table. Global temp tables are defined with a double ## instead of one.
There is an article here from SQLTeam regarding temp tables and also here from MSDN