68

Can someone please explain to me what the # symbol means in MS SQL Code.

I've tried Googling it, and even searching on StackOverflow, but can't seem to find the answer.

I feel like an idiot - having one of "those" days. Please help.

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Saajid Ismail
  • 8,029
  • 11
  • 48
  • 56

5 Answers5

58

They normally prefix temporary tables.

From the docs....

Prefix local temporary table names with single number sign (#table_name), and prefix global temporary table names with a double number sign (##table_name).

Ed Guiness
  • 34,602
  • 16
  • 110
  • 145
  • 1
    Perhaps better to post a link for SQL Server 2005/2008 nowadays, even if info is still the same...? – gbn Jul 02 '10 at 13:50
  • 1
    What are the differences between this and a table variable via DECLARE? – ryanwebjackson Dec 18 '18 at 19:00
  • What about hashtags after the name?, such as here: https://learn.microsoft.com/en-us/sql/t-sql/functions/row-number-transact-sql?view=sql-server-ver16 SELECT ROW_NUMBER() OVER(ORDER BY name ASC) AS Row#, name, recovery_model_desc FROM sys.databases – skan Jun 22 '23 at 06:50
  • 1
    @skan In that example the # is part of the name, and has no special meaning. – Ed Guiness Jun 24 '23 at 08:32
13

The pound sign # is used to prefix temporary tables and procedures. A single instance (#) refers to a temporary object that lives/dies with the current session while a double instance (##) is a global object.

Michael Haren
  • 105,752
  • 40
  • 168
  • 205
10

The other answers are correct if you're dealing with SQL Server, and it's clear that you are. But since the question title just says SQL, I should mention that there are some forms of SQL such as MySQL where a pound sign is used as an alternative commenting symbol.

#This is a comment.

https://dev.mysql.com/doc/refman/5.7/en/comments.html

Kyle Delaney
  • 11,616
  • 6
  • 39
  • 66
5

You could be seeing the # in the usage of a temporary table

SELECT
  *
FROM #myTempTable
John Hartsock
  • 85,422
  • 23
  • 131
  • 146
0

#TEMPTABLE. Its actually a type of a temporary table that is scoped for that session.

Baaju
  • 1,992
  • 2
  • 18
  • 22