#TempTables 的范围仅限于 EXEC 语句?

发布于 2024-09-18 13:18:08 字数 367 浏览 11 评论 0原文

通过尝试这个,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

会傲 2024-09-25 13:18:08

是的。

来查看它

exec('
select * into #TempTable2 from sys.all_views 
select * from tempdb.sys.tables
')
select * from tempdb.sys.tables

如果您希望稍后能够访问它,您可以使用 ##global 临时表 。例如

use master
go
declare @temptablename char(40)

set @temptablename = '[##' + cast(newid() as char(36)) + ']'

exec('
select * into ' + @temptablename + ' from sys.all_views 
')
/* Do some other stuff: */

exec('
select * from ' + @temptablename)

Yes.

You can see this with

exec('
select * into #TempTable2 from sys.all_views 
select * from tempdb.sys.tables
')
select * from tempdb.sys.tables

You can use a ##global temporary table if you want to be able to access it later. e.g.

use master
go
declare @temptablename char(40)

set @temptablename = '[##' + cast(newid() as char(36)) + ']'

exec('
select * into ' + @temptablename + ' from sys.all_views 
')
/* Do some other stuff: */

exec('
select * from ' + @temptablename)
风轻花落早 2024-09-25 13:18:08

您可以创建一个全局临时表

create table ##TempTable (
    /* Structure goes here */
)

exec('insert into ##TempTable(/*Columns*/) select * from sys.all_views')

select * from ##TempTable

(如果您这样做,并且您的代码可能由多个用户使用,则在临时表中包含 SPID 列,在选择列表中包含 @@SPID,并将最终选择更改为按SPID=@@SPID)

You could create a global temp table

create table ##TempTable (
    /* Structure goes here */
)

exec('insert into ##TempTable(/*Columns*/) select * from sys.all_views')

select * from ##TempTable

(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)

隱形的亼 2024-09-25 13:18:08

是的,你是对的。 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文