SQL Server 中派生表的范围

发布于 2024-09-01 22:34:02 字数 308 浏览 2 评论 0原文

我最近一直在研究 SQL 并进行一些探索。关于临时表,我发现了 3 种不同的临时表类型:

1) CREATE TABLE #TempTable

2) DECLARE TABLE @TempTable

3) SELECT * FROM (SELECT * FROM Customers) AS TempTable

现在我了解了 #TempTable 和@TempTable 类型,但是示例 3 中的派生表又如何呢?这个派生表存储在哪里?如果它在 1 个事务中声明,第二个事务是否可以访问它,或者派生表的范围与示例 1 和 2 相同吗?

I've been looking into SQL recently and exploring a bit. in regards to Temp Tables I have discovered 3 different temp table types:

1) CREATE TABLE #TempTable

2) DECLARE TABLE @TempTable

3) SELECT * FROM (SELECT * FROM Customers) AS TempTable

Now I understand the scope behind the #TempTable and the @TempTable types, but what about the derived table as in example 3? Where does this derived table get stored? and if it is declared in 1 transaction, can a 2nd transaction access it, or is the scoping of Derived Tables that same as example 1 and 2?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

北笙凉宸 2024-09-08 22:34:02

1) CREATE TABLE #TempTable --local temp table

本地临时表仅在当前会话中可见,并且可以在嵌套存储过程调用之间共享: sommarskog.se/share_data.html#temptables" rel="nofollow noreferrer">http://www.sommarskog.se/share_data.html#temptables

2) DECLARE TABLE @TempTable --本地表变量

局部变量的作用域是声明它的批处理、存储过程或语句块。它们可以作为参数在过程之间传递。它们不受事务的影响,并将在回滚后保留所有行。

3) SELECT * FROM (SELECT * FROM Customers) AS TempTable --派生表

仅对当前查询可见

4) CREATE TABLE ##TempTable --全局临时表

这与 #temp 表不同,因为它对所有进程都是可见的。当创建过程结束时,该表将被删除(但将等待其他进程的任何当前活动完成)。

5) CTE - 公共表表达式

示例 CTE:

;WITH YourBigCTE AS
(
    big query here

)
SELECT * FROM YourTable1  WHERE ID IN (SELECT ID FROM YourBigCTE)
UNION 
SELECT * FROM YourTable2  WHERE ID IN (SELECT ID FROM YourBigCTE)

可以在同一个 CTE 命令中多次使用,甚至可以递归使用,并且将在 CTE 命令的持续时间内持续使用。

1) CREATE TABLE #TempTable --local temp table

Local temporary tables are visible only in the current session, and can be shared between nested stored procedure calls: http://www.sommarskog.se/share_data.html#temptables

2) DECLARE TABLE @TempTable --local table variable

The scope of a local variable is the batch, stored procedure, or statement block in which it is declared. They can be passed as parameters between procedures. They are not subject to transactions and will retain all rows following a rollback.

3) SELECT * FROM (SELECT * FROM Customers) AS TempTable --derived table

is visible to the current query only

4) CREATE TABLE ##TempTable --global temp table

This differs from a #temp table in that it is visible to all processes. When the creating process ends, the table is removed (but will wait until any current activity from other processes is done).

5) CTE - common table expression

example CTE:

;WITH YourBigCTE AS
(
    big query here

)
SELECT * FROM YourTable1  WHERE ID IN (SELECT ID FROM YourBigCTE)
UNION 
SELECT * FROM YourTable2  WHERE ID IN (SELECT ID FROM YourBigCTE)

can be used multiple times within the same CTE command, even recursively, and will last for the duration of the CTE command.

吃→可爱长大的 2024-09-08 22:34:02

这取决于您使用的实际 RDBMS - 我假设 Sybase 或 SQL Server(但很可能对所有情况都是如此)

SELECT * FROM (SELECT * FROM Customers) AS TempTable

Temptable 仅适用于当前 SQL 语句,即仅适用于选择。

SELECT * FROM Customers into tempdb..TempTable

将在 tempdb 中创建一个新表,必须显式删除该表

This depends on the actual RDBMS you are using - I will assume Sybase or SQL Server (but might well be true for all)

SELECT * FROM (SELECT * FROM Customers) AS TempTable

Temptable is only available for the current SQL statement ie just the select.

SELECT * FROM Customers into tempdb..TempTable

would create a new table in the tempdb whichg would have to be explicitly dropped

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