SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Temporary Objects 2


Temporary Objects 2

Author
Message
sqlmunkee
sqlmunkee
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1305 Visits: 286
Even if you hadn't added the constraint to the table, the second program would still fail because there would already be the temp table from the first program.

I wrote a small bit on my blog about what goes on in tempdb as regards temporary object names, which kind of touches on why you can have multiple table variables of the same name, but not multiple temp tables.

http://sqlmunkee.blogspot.com/2011/09/temporary-variable-couple-of-points.html

sqlmunkee
Bringing joy and happiness via SQL Server since 1998
Toreador
Toreador
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 5985 Visits: 8281
sqlmunkee (3/9/2012)
Even if you hadn't added the constraint to the table, the second program would still fail because there would already be the temp table from the first program.


That's not true. You can create as many identically named temp tables as you want provided they are in different sessions (even 2 different connections for the same user).
lightyun
lightyun
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 193
Toreador (3/9/2012)
sqlmunkee (3/9/2012)
Even if you hadn't added the constraint to the table, the second program would still fail because there would already be the temp table from the first program.


That's not true. You can create as many identically named temp tables as you want provided they are in different sessions (even 2 different connections for the same user).


yes, the temp table with same name can definitely existed in different sessions.
david-739410
david-739410
SSC Veteran
SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)

Group: General Forum Members
Points: 257 Visits: 128
I agree with monkeyloafer the answer is wrong
sqlmunkee
sqlmunkee
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1305 Visits: 286
You're right, and for the reasons in my blog post :-)

My bad. Sorry, I'll get my coat.

sqlmunkee
Bringing joy and happiness via SQL Server since 1998
bitbucket-25253
bitbucket-25253
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35141 Visits: 25280
Hugo Kornelis (3/9/2012)
I am embarassed to admit that I got this question, that apparently was based on my comments, wrong.

I should have been warned. I even checked the code several times. All because I thought that if both programs run without error (as I assumed), there would have been THREE correct answers. So I checked. And checked again.

I did contemplate the extra comma, but figured that this would not be Ron's style, so it had to be a genuine type (hint: test all code before submitting QotD!)
I did also contemplate the inconsistent use of upper- and lowercase, but figure that wouldn't be Ron's style either (hint: test all code on a database with case-sensitive collation)

And after reading the script yet another time, I ticked the first three options - and then kicked myself for overlooking the named constraint.

Thanks, Ron! Good question (though unfortunately hampered by the typo and the inconsistent upper-/lowercase).


I am too use to using a case insensitive collation .. another bad habit that I have developed. I did test the code BEFORE submitting for a QOD, and that extra comma must have slipped in at that time, now that is not a valid excuse (as I well know), what can I say except things happen, and it is my fault for which I apologize to all.

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Tom Thomson
Tom Thomson
SSC Guru
SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)

Group: General Forum Members
Points: 51102 Visits: 13160
Good straightforward question and answer.

Easy for me, as it was from Hugo's first comment that I first discovered that people could have named constraints (instead of letting SQLServer generate the constraint names) on temporary tables, and that was sufficiently recent (just last November) for me not to have forgotten the shock (shock because I could see where it would probably lead and so immediately tested it, with a result that was confirmed by Hugo's second comment in that discussion).

Tom

bitbucket-25253
bitbucket-25253
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35141 Visits: 25280
L' Eomot Inversé (3/9/2012)
Good straightforward question and answer.

Easy for me, as it was from Hugo's first comment that I first discovered that people could have named constraints (instead of letting SQLServer generate the constraint names) on temporary tables, and that was sufficiently recent (just last November) for me not to have forgotten the shock (shock because I could see where it would probably lead and so immediately tested it, with a result that was confirmed by Hugo's second comment in that discussion).


I also followed your path, especially since it was Hugo commenting on another QOD that I had published. The number of comments for that QOD was rather sparse and I felt that more people should be aware of the pitfalls of what at first appearence appears to be innocuous. Hence this QOD.

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Rich Mechaber
Rich Mechaber
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5789 Visits: 3691
I think of this having less to do with temporary objects so much as it does with schema restrictions: you can't have 2 objects with the same name in the schema. But it's easier to make the mistake when creating temp tables, b/c you can create many temp tables with the "same" name.

I found posted examples of this problem here: http://sequelserver.blogspot.com/2006/12/named-constraints-on-temp-tables.html and here: http://www.sqlservercentral.com/articles/63472/.

Also, in my haste, I overlooked the explicitly named constraint in the question, and I was about to check the first answer (both sessions execute successfully), but you gave it away by telling us to "SELECT THE CORRECT 2 ANSWERS". So I sharpened my eyeballs! Thanks for the question,

Rich
John-150025
John-150025
SSC-Addicted
SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)SSC-Addicted (449 reputation)

Group: General Forum Members
Points: 449 Visits: 223
Typo in the question!!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search