Home » Software

SQL Tempdb and Constraint Names

Submitted by admin on May 31, 2010 – 2:27 pmNo Comment

A weird thing I found out about MS-SQL’s tempdb a few days ago was that the server does not hash the constraint names.

I don’t know if you know the way tempdb is working in MS-SQL but the basic concept is that when a process is creating a temp table the tempdb is hashing the name to create a unique instance for the running session.

so if you write a code that looks like this:

create table #TestTabe (id int, descript nvarchar(50))

in runtime you will see in the tempdb something that looks like this:

#TestTable________________123213E2B

the main reason is that you can have multiple sessions running at the same time.

Let’s add a constraint to the equation:

CREATE TABLE #TestTable(

id int, descript nvarchar(50),

CONSTRAINT [PK_#TestTable] PRIMARY KEY CLUSTERED

(

[id] ASC

)

)

in runtime you will see something like this:

The normal way tempdb is operating is when the session ends (i.e. stored procedure finished it’s execution) or when the user manually drop the table it’s deleted from the tempdb with all of it’s constraints.

if your session fails for some reason and you didn’t catch it the tempdb is not cleaned and the table (with the hashed name) and it’s constraints are left in the tempdb.

This is not a big deal in general cause the table name is hashed for each session so the next session will have no problem creating it’s own instance of the table.

The weird stuff I found out and kind’a puzzled me was that MSSQL does not hash the constraint name! so the next session that tried to create the table FAILD with the error of “Constraint already exists in the db”!

So until the next cleanup of the tempdb (restart of the sql server should do the job) the process is stuck and will fail every time!

The solution?

Wrap your code with try & catch so you can drop all the temp tables in the case of a failure! The code I saw (not my code of course:-) )was not wrapped and this created an issue.

That’s it for today.

Till the next time, keep coding…

Joseph Gozlan, Information Systems Engineer.
http://www.JosephGozlan.com

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.


Architecture »

Want Imperia 3 BHK apartments Dosti Group THANE MUMBAI

Affinity Solution Pvt. Ltd.
Presents
Dosti Imperia Thane Mumbai:
Dosti Group Imperia Thane ||91-9999684166|| Dosti Group Imperia Project Thane | Dosti Group Imperia Property Thane | Dosti Group Imperia Propeties Thane | Dosti Group Imperia Apartment Thane | …

Hi-Tech »

IT »

Benefits of Online Video on Demand Services for Individuals

Are you tired of waiting for your favorite programs or movies? On the other hand, do you have to wait restlessly in front of the idiot box and have to watch commercials in between your …

Medicine »

Bring your natural smile back by consulting Burbank cosmetic dentist

A beautiful and charming smile is all you need when you speak to somebody. Your smile can attract anyone and make an impressive impact to the listener. And a simple problem in your teeth and …

Press Releases »

Vashi Navi Mumbai New Booking Properties-09999684166 Sparsh Projects By Arihant Vashi Mumbai

Affinity Solution Pvt. Ltd.
Presents
Arihant Sparsh Vashi Navi Mumbai:
Arihant Sparsh | Arihant Sparsh Vashi | Arihant Sparsh Vashi Rates | Arihant Sparsh Vashi Navi Mumbai | Arihant Sparsh Vashi Location | Arihant Sparsh Project Vashi | …

Science »

Culinary Management Programs

There are many different culinary management programs throughout the country at various schools, but the most common type is the Bachelor of Culinary Management degree, which can be found at colleges, universities, technical institutions and …