0

What are the differences between the following 3 in sql-server 2008?

#Table_name
##Table_name
@Table_name
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Peter
  • 351
  • 1
  • 5
  • 14
  • http://sqlserverplanet.com/tsql/yet-another-temp-tables-vs-table-variables-article – Tim Schmelter Feb 21 '13 at 16:10
  • 2
    @TimSchmelter That repeats some inaccuracies found elsewhere on the web, the most glaring being the claim that operations on table variables are not logged. I commented to that effect a couple of months ago and my comment was never published. I believe my answer here to be one of the most accurate and comprehensive comparisons. http://dba.stackexchange.com/questions/16385/whats-the-difference-between-a-temp-table-and-table-variable-in-sql-server/16386#16386 – Martin Smith Feb 22 '13 at 15:27

3 Answers3

1

From MSDN: Database Identifiers (reformatted to better suit the question).

Certain symbols at the beginning of an identifier have special meaning in SQL Server.

  • @: A regular identifier that starts with the at sign always denotes a local variable or parameter and cannot be used as the name of any other type of object.
  • #: An identifier that starts with a number sign denotes a temporary table or procedure.
  • ##: An identifier that starts with double number signs (##) denotes a global temporary object. Although the number sign or double number sign characters can be used to begin the names of other types of objects, we do not recommend this practice.
  • @@: Some Transact-SQL functions have names that start with double at signs (@@). To avoid confusion with these functions, you should not use names that start with @@.
MicSim
  • 26,265
  • 16
  • 90
  • 133
0

See this Question

https://stackoverflow.com/a/64891/886591

#table = temp table tied to the spid
##table = global temp table
@table = table variable
Community
  • 1
  • 1
Lance
  • 3,193
  • 2
  • 32
  • 49
0

#table_name - Local Temp Table

  • It exists only for the duration of a user session or the procedure that created the temporary table
  • When the user logs off or when the procedure that created the table completes, the local temporary table is lost

##table_name - global temporary table

  • It exists for the duration of a user session
  • When the last user session that references the table disconnects, the global temporary table is lost

@Table_name - table-variable - It also exists only for the duration of a user session

Temp Table and Table Variable — both are created in TempDB and not in memory

Pandian
  • 8,848
  • 2
  • 23
  • 33