Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

What is # in first letter of table names? Expand / Collapse
Author
Message
Posted Friday, July 05, 2013 6:34 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, March 21, 2014 1:28 AM
Points: 59, Visits: 269
Ive noticed dudes in this forum uses # for the first letter of their table names.
Does it have any special meaning?
Does SQL Server have different behavior with them?
Or its just a letter like a,b,c... etc?


___________________________________
Computer Enterprise Masoud Keshavarz
I don't care about hell.
If I go there I've played enough Diablo to know how to fight my way out.
Post #1470698
Posted Friday, July 05, 2013 6:50 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 4:02 AM
Points: 12,206, Visits: 9,171
These are temporary tables.
Basically it means they are stored in tempdb (one of the system databases) and are dropped when the connection is terminated or when SQL Server is restarted.

They behave like any other table, so you can create them with CREATE TABLE #myTempTable or with SELECT ... INTO #myTempTable.
You can use ALTER TABLE and add indexes if you want.




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1470712
Posted Friday, July 05, 2013 6:53 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 12:01 PM
Points: 12,744, Visits: 31,074
masoudk1990 (7/5/2013)
Ive noticed dudes in this forum uses # for the first letter of their table names.
Does it have any special meaning?
Does SQL Server have different behavior with them?
Or its just a letter like a,b,c... etc?


#table is a temporary table...it's one that you can create on the fly, and it gets destroyed automatically when your connection is closed.

SELECT name
INTO #temp
from sys.tables


they are real tables, and are unique to the processes/scope that created them...so a procedure which creates a temp table can be called thousands of times per second, and each instance of the procedure creates it's own temp table, called #temp or whatever, but that do not collide with any other process creating temp tables by the same name.

technically, a table gets created with a unique name in tempdb, and it might actually be called the same name, but with a unique number appended to the end of it;
see for yourself:
select POWER(convert(bigint,2),31)  as val into #temp

select name from tempdb.sys.tables where name LIKE '#temp%'
--#temp__{snip}__000000000006



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1470715
Posted Friday, July 05, 2013 6:55 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 7:59 AM
Points: 2,078, Visits: 2,410
When you put a # sign in front of the table name, the table will be created in [tempdb] database. This table will be only available within your current session and is called a local temporary table. When your session is ended, the temporary table will be automaticly removed.
If you put a double # sign in front of the table name, the table will also be created in [tempdb] database and is called a global temporary table. This table will be available from within other sessions.

From MSDN:
Temporary Tables
--------------------------------------------------------------------------------
There are two types of temporary tables: local and global. Local temporary tables are visible only to their creators during the same connection to an instance of SQL Server as when the tables were first created or referenced. Local temporary tables are deleted after the user disconnects from the instance of SQL Server. Global temporary tables are visible to any user and any connection after they are created, and are deleted when all users that are referencing the table disconnect from the instance of SQL Server.


** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
Post #1470716
Posted Friday, July 05, 2013 6:55 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 4:02 AM
Points: 12,206, Visits: 9,171
select POWER(convert(bigint,2),31)  as val into #temp

I see what you did there
Recycling is good for the environment




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1470717
Posted Friday, July 05, 2013 7:06 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 12:01 PM
Points: 12,744, Visits: 31,074
Koen Verbeeck (7/5/2013)
select POWER(convert(bigint,2),31)  as val into #temp

I see what you did there
Recycling is good for the environment


haha! cannot put anything past you! at least i didn't plagiarize!


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1470722
Posted Friday, July 05, 2013 8:02 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, March 21, 2014 1:28 AM
Points: 59, Visits: 269
Thank you every one.


@HanShi
If you put a double # sign in front of the table name, the table will also be created in [tempdb] database and is called a global temporary table. This table will be available from within other sessions


Thank you for extra information.

@Koen Verbeeck

I see what you did there [Wink]
Recycling is good for the environment [BigGrin]


What did Lowell do?
You made me afraid to test his query.



___________________________________
Computer Enterprise Masoud Keshavarz
I don't care about hell.
If I go there I've played enough Diablo to know how to fight my way out.
Post #1470747
Posted Friday, July 05, 2013 8:18 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 12:01 PM
Points: 12,744, Visits: 31,074
select POWER(convert(bigint,2),31)  as val into #temp

he was referencing another thread, where someone asked for the max size of a varchar(max);
that thread is here:
http://www.sqlservercentral.com/Forums/Topic1470699-391-1.aspx

i reused my same code example from that thread, but inserted it into a #temp table to show you how to use a temp table;

he was referring to the fact that he noticed the same code in the other thread.

it was just a for-fun notification that he sees everything.


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1470753
Posted Friday, July 05, 2013 10:27 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 4:02 AM
Points: 12,206, Visits: 9,171
Lowell (7/5/2013)

it was just a for-fun notification that he sees everything.


That's true. So you better watch out!

(maybe I'll add an off-topic warning next time, to not confuse anyone else)




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1470809
Posted Tuesday, July 09, 2013 8:16 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, April 17, 2014 2:02 PM
Points: 500, Visits: 2,291
HanShi mentioned local and global temp tables. You don't see this as often but a global temp table has two numbers signs in front of it like so: ##temptable.

To get some understanding about the difference between the two, open a new query window in SSMS and run these SELECT statements:

CREATE TABLE #LocalTempTable	(xId int);	--(1) Local Temp Table
CREATE TABLE ##GlobalTempTable (xId int); --(2) Global Temp Table

INSERT INTO #LocalTempTable VALUES(1);
INSERT INTO ##GlobalTempTable VALUES(1);

Then, in the same query window, you could successfully run these queries:

SELECT * FROM ##GlobalTempTable
SELECT * FROM #LocalTempTable

If you open a new query window, however, and run the two SELECT statements above, the first will be successful but the second one will fail.


-- Alan Burstein



Read this article for best practices on asking questions.
Need to split a string? Try this (Jeff Moden)
Need a pattern-based string spitter? Try this (Dwain Camps)
My blog
Post #1471644
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse