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 ««1234»»»

Temporary Objects 2 Expand / Collapse
Author
Message
Posted Friday, March 9, 2012 3:59 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, June 30, 2014 9:28 AM
Points: 379, Visits: 232
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
Post #1264225
Posted Friday, March 9, 2012 4:02 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 8:40 AM
Points: 1,715, Visits: 6,265
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).
Post #1264227
Posted Friday, March 9, 2012 4:16 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, November 29, 2013 3:07 AM
Points: 11, Visits: 126
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.
Post #1264232
Posted Friday, March 9, 2012 4:19 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, July 21, 2014 10:10 AM
Points: 71, Visits: 58
I agree with monkeyloafer the answer is wrong
Post #1264235
Posted Friday, March 9, 2012 4:32 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, June 30, 2014 9:28 AM
Points: 379, Visits: 232
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
Post #1264241
Posted Friday, March 9, 2012 5:21 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:13 PM
Points: 5,575, Visits: 24,826
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
Post #1264269
Posted Friday, March 9, 2012 5:21 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 4:59 PM
Points: 8,576, Visits: 9,089
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
Post #1264270
Posted Friday, March 9, 2012 5:49 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:13 PM
Points: 5,575, Visits: 24,826
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
Post #1264299
Posted Friday, March 9, 2012 6:30 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Thursday, July 17, 2014 2:01 PM
Points: 687, Visits: 3,002
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

Post #1264330
Posted Friday, March 9, 2012 6:54 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, July 14, 2014 9:58 AM
Points: 43, Visits: 205
Typo in the question!!
Post #1264346
« Prev Topic | Next Topic »

Add to briefcase ««1234»»»

Permissions Expand / Collapse