是否有必要使用 # 在 SQL Server 中创建临时表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16749045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 15:24:25  来源:igfitidea点击:

Is it necessary to use # for creating temp tables in SQL server?

sqlsql-serversql-server-2008sql-server-2005

提问by Arun CM

Is it necessary to use #before creating a temporary table in SQL server?

#SQL server 中创建临时表之前是否需要使用?

Example:

例子:

SELECT column1, column2, someInt, someVarChar 
INTO ItemBack1 
FROM table2
WHERE table2.ID = 7

For ItemBack1 is it necessary to use the #symbol?

对于 ItemBack1 是否需要使用#符号?

If not, then what is the use of #in creating temp tables?

如果不是,那么#创建临时表有什么用?

回答by mobiledaemon

Yes. You need to prefix the table name with "#" (hash) to create temporary tables.

是的。您需要在表名前加上“#”(哈希)来创建临时表。

If you do NOT need the table later, go ahead & create it. Temporary Tables are very much like normal tables. However, it gets created in tempdb. Also, it is only accessible via the current session i.e. For EG: if another user tries to access the temp table created by you, he'll not be able to do so.

如果您以后不需要该表,请继续创建它。临时表与普通表非常相似。但是,它是在 tempdb 中创建的。此外,它只能通过当前会话访问,即对于 EG:如果另一个用户试图访问您创建的临时表,他将无法这样做。

"##" (double-hash creates "Global" temp table that can be accessed by other sessions as well.

“##”(双哈希创建“全局”临时表,其他会话也可以访问该表。

Refer the below link for the Basics of Temporary Tables: http://www.codeproject.com/Articles/42553/Quick-Overview-Temporary-Tables-in-SQL-Server-2005

有关临时表的基础知识,请参阅以下链接:http: //www.codeproject.com/Articles/42553/Quick-Overview-Temporary-Tables-in-SQL-Server-2005

If the content of your table is less than 5000 rows & does NOT contain data types such as nvarchar(MAX), varbinary(MAX), consider using Table Variables.

如果表的内容少于 5000 行且不包含 nvarchar(MAX)、varbinary(MAX) 等数据类型,请考虑使用表变量。

They are the fastest as they are just like any other variables which are stored in the RAM.They are stored in tempdb as well, not in RAM.

它们是最快的,因为它们就像存储在 RAM 中的任何其他变量一样。它们也存储在 tempdb 中,而不是存储在 RAM 中

DECLARE @ItemBack1 TABLE
(
 column1 int,
 column2 int,
 someInt int,
 someVarChar nvarchar(50)
);

INSERT INTO @ItemBack1
SELECT column1, 
       column2, 
       someInt, 
       someVarChar 
  FROM table2
 WHERE table2.ID = 7;

More Info on Table Variables: http://odetocode.com/articles/365.aspx

有关表变量的更多信息:http: //odetocode.com/articles/365.aspx

回答by zzlalani

The difference between this two tables ItemBack1and #ItemBack1is that the first on is persistent (permanent) where as the other is temporary.

这两个表之间的差值ItemBack1,并#ItemBack1在于,第一上是持久的(永久的),其中作为另一种是暂时的。

Now if take a look at your question again

现在如果再看看你的问题

Is it necessary to Use # for creating temp table in sql server?

是否有必要使用 # 在 sql server 中创建临时表?

The answer is Yes, because without this preceding #the table will not be a temporary table, it will be independent of all sessions and scopes.

答案是Yes,因为没有这个前面#的表将不是临时表,它将独立于所有会话和范围。