views:

124

answers:

5

I wanted to know what does the # symbol mean? Why is it being used?

Example:

INSERT INTO #tmpContracts (sym, contractCount)
SELECT sym, 
       COUNT(DISTINCT(fulloptionsym)) AS contractCount
+5  A: 

It means it is a temporary table. Take a look at the "Temporary Table" section of the CREATE TABLE documentation.

David
+4  A: 

It is a temporary table, and is only available to the current session. It will be automatically dropped when it goes out of scope. If the same code is run by multiple people at the same time, each gets their own table, like local variables, and there is no need to worry about a collision.

KM
+1 That is the reason it is being used, so that multiple users can use the stored procedure at the same time without conflict or having to manage potential collisions.
Yishai
This aswer is more understandable cause refers about database scope objects
Angel Escobedo
+1  A: 

A #Temp is a temp table that is active for the duration of the connection. It resides in the TempDB.

A #Temp is local (only available to the current connection) and is dropped once the connection is terminated.

A ##Temp is global (available to all connections) and is dropped once all connections using it are terminated.

corrected owing to feedback and correction from Gail

There is also a ## table type in SQL Server which is a temp table that is accessible removed when the server or the SQL Server service is restarted.

Raj More
## tables aren't removed at server restart. They're removed once the connection that created them has closed and no one else is using it.
GilaMonster
Thank you Gail. That cleared a huge misconception of mine.
Raj More
A temporary table's lifetime is scoped to the connection/session if it is created at the session-level (outside of a stored procedure). However, if it is created within a stored procedure, then It's lifetime is scoped to the lifetime of that stored procedure's invocation (i.e., the current request/batch).
RBarryYoung
+4  A: 

'#' is used for temporary table e.g : '#tmpContracts' '##' is used for global temporary table e.g : '##tmpContracts'

Read this temporary table

anishmarokey
A: 

'#temp' = temperory table

NinethSense