SQL 2008 中的临时表和表变量有什么区别?

发布于 2024-08-08 14:03:33 字数 222 浏览 6 评论 0原文

之间有什么区别?

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

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

发布评论

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

评论(4

忆梦 2024-08-15 14:03:33

临时表在大多数特性上与普通表类似,只是它们进入 TempDB 而不是当前数据库,并且在有限范围后消失(取决于它们是基于会话的临时表还是全局临时表。但是对临时表中数据的所有更改都是 添加一样。

记录到事务日志中,以及由此带来的所有性能影响,您还可以向临时表添加任意数量的索引、视图、触发器或任何其他内容,就像向普通表变量 是一种快捷内存表(它们也使用临时数据库)不会记录对它们的更改(这可以提高性能),但您只能在它们上获得一个索引(因为在初始索引之后无法创建索引)。声明语句,您可以在表变量上创建的唯一索引是可以包含在初始表变量声明中的索引...

   Declare @Tab Table (myKey integer Primary Key Not Null, data varchar(20)) 

由于这些特性,临时表是大型表(宽且行数多)的更好选择,和/或在其生命周期内将经历不止一种访问模式,而当您需要一个非常窄的表(仅键表,或仅具有一个数据列的键)时,表变量是最好的选择,该表将始终由索引访问钥匙...

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...

   Declare @Tab Table (myKey integer Primary Key Not Null, data varchar(20)) 

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...

甜点 2024-08-15 14:03:33

这是关于不同临时表的一个很好的参考

临时表与变量

This is a pretty good reference on the different temp tables

Temp Tables vs Variables

多彩岁月 2024-08-15 14:03:33
  1. 表变量没有日志
  2. 表变量仅具有局部作用域(您无法从不同的过程访问相同的表变量)
  3. 无法预编译带有临时表的过程

有关更多详细信息,请参见 此主题

  1. There is no log for table variables
  2. Table variables have only local scope (you cannot access the same table variable from different procedures)
  3. Procedures with temporary tables cannot be pre-compiled

For more details see this topic.

烟酉 2024-08-15 14:03:33
  1. 表变量具有明确定义的范围。它们将在批次(即当前批次的语句)结束时自动清除,其中临时表对当前会话和嵌套存储过程可见。全局临时表将对所有会话可见。

  2. 表变量是使用 Declare 语句创建的。我们无法使用语句创建表变量

    选择*到@tableVariableName中
    

    但是我们可以使用Create table语句和语句来创建临时表

    选择 * 进入 #tempTableName
    
  3. 在SQL Server 2008 中,我们可以将表变量作为参数传递给存储过程。但是我们不能将临时表作为参数传递给存储过程。

  4. 我们可以在UDF(用户定义函数)内部使用表变量,但不能在UDF内部使用临时表。

    我们可以在

  1. 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.

  2. Table variables are created using Declare statement. We can't create table variable using statement

    select * into @tableVariableName
    

    But we can create temporary table using Create table statement as well as statement

    select * into #tempTableName
    
  3. 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.

  4. We can use the table variable inside the UDF (user defined function) but we can't use the temporary table inside the UDF.

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