SQL 2008 中的临时表和表变量有什么区别?
之间有什么区别?
CREATE TABLE #temp ( [ID] INT)
INSERT INTO #temp
SELECT ...
的和
DECLARE @temp TABLE ( [ID] INT)
INSERT @temp
SELECT ...
SQL Server 2008 中
What is the difference between:
CREATE TABLE #temp ( [ID] INT)
INSERT INTO #temp
SELECT ...
and
DECLARE @temp TABLE ( [ID] INT)
INSERT @temp
SELECT ...
in SQL Server 2008?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
临时表在大多数特性上与普通表类似,只是它们进入 TempDB 而不是当前数据库,并且在有限范围后消失(取决于它们是基于会话的临时表还是全局临时表。但是对临时表中数据的所有更改都是 添加一样。
记录到事务日志中,以及由此带来的所有性能影响,您还可以向临时表添加任意数量的索引、视图、触发器或任何其他内容,就像向普通表变量 是一种快捷内存表(它们也使用临时数据库)不会记录对它们的更改(这可以提高性能),但您只能在它们上获得一个索引(因为在初始索引之后无法创建索引)。声明语句,您可以在表变量上创建的唯一索引是可以包含在初始表变量声明中的索引...
由于这些特性,临时表是大型表(宽且行数多)的更好选择,和/或在其生命周期内将经历不止一种访问模式,而当您需要一个非常窄的表(仅键表,或仅具有一个数据列的键)时,表变量是最好的选择,该表将始终由索引访问钥匙...
Temporary tables are like ordinary tables in most characteristics, except they go into TempDB instead of the current Database, and they dissapear after limited scope, (depending on whether they are session based or global Temp Tables. But all changes to data in Temp tables is logged to the transaction log, with all the performance implications that that entails. otoh, you can also add as many indices or views, or triggers, or whatever else you want to a temp table exactly as you would to a ordinary table.
Table variables are a kind of short-cut in-memory table (they also use temp DB). Changes to them are not logged (this improves performance). But you can only get one index on them, (because indices cannot be created after the initial declaration statement, the only index you can create on a table variable is the one that can be included in the initial table variable declaration...
Because of these characteristics, temp tables are better choice for large tables, (wide and with many rows), and/or that will undergo more than one access pattern during their lifetime, whilst table variables are best when you need a very narrow table (keys only table, or key with only one data column), which will always be accessed by that indexed key...
这是关于不同临时表的一个很好的参考
临时表与变量
This is a pretty good reference on the different temp tables
Temp Tables vs Variables
有关更多详细信息,请参见 此主题。
For more details see this topic.
表变量具有明确定义的范围。它们将在批次(即当前批次的语句)结束时自动清除,其中临时表对当前会话和嵌套存储过程可见。全局临时表将对所有会话可见。
表变量是使用 Declare 语句创建的。我们无法使用语句创建表变量
但是我们可以使用Create table语句和语句来创建临时表
在SQL Server 2008 中,我们可以将表变量作为参数传递给存储过程。但是我们不能将临时表作为参数传递给存储过程。
我们可以在UDF(用户定义函数)内部使用表变量,但不能在UDF内部使用临时表。
我们可以在
Table variables have a well defined scope. They will be cleared automatically at the end of the batch (i.e current batch of statements) where as temporary table will be visible to current session and nested stored procedures. Global Temporary table will be visible to the all the sessions.
Table variables are created using Declare statement. We can't create table variable using statement
But we can create temporary table using Create table statement as well as statement
In SQL Server 2008 onwards we can pass the table variable as a parameter to the stored procedures. But we can't pass the temporary table as a parameter to the stored procedure.
We can use the table variable inside the UDF (user defined function) but we can't use the temporary table inside the UDF.