Unix Technical Forum

create constraint error

This is a discussion on create constraint error within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi. I have a procedure with this in it (there are no other references to asset_number_bak_tmp_pk in the procedure ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 03:05 PM
Jeff Kish
 
Posts: n/a
Default create constraint error

Hi.

I have a procedure with this in it (there are no other references to
asset_number_bak_tmp_pk in the procedure and it calls nothing else written by
me, just system calls or normal dml).

create table #asset_bak(
asset_number varchar(60) not null,
asset_desc varchar(100) null,
location varchar(40) null,
constraint asset_number_bak_tmp_pk primary key clustered (asset_number))

When I run the procedure, I get this message:

(1 row(s) affected)
Msg 2714, Level 16, State 4, Procedure updatenavharrierdb, Line 19
There is already an object named 'asset_number_bak_tmp_pk' in the database.
Msg 1750, Level 16, State 0, Procedure updatenavharrierdb, Line 19
Could not create constraint. See previous errors.

How can I find where else the system thinks this constraint exists?


I tried this but it only finds it in one place (one row in the result set),
i.e. my procedure:

select sysobjects.name, syscomments.text
from sysobjects, syscomments
where sysobjects.id = syscomments.id and
((lower(sysobjects.name) like '%asset_number_bak_tmp_pk%') or
(lower(syscomments.text) like '%asset_number_bak_tmp_pk%'))

Is this somehow a case where I need to do something dynamically, or purge some
information? I thought temp tables and their crony constraints disappeared
after the procedure exited.

thanks
Jeff Kish
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 03:05 PM
Jeff Kish
 
Posts: n/a
Default Re: create constraint error

On Wed, 16 May 2007 08:41:50 -0400, Jeff Kish <jeff.kish@mro.com> wrote:

>Hi.
>
>I have a procedure with this in it (there are no other references to
>asset_number_bak_tmp_pk in the procedure and it calls nothing else written by
>me, just system calls or normal dml).
>
>create table #asset_bak(
> asset_number varchar(60) not null,
> asset_desc varchar(100) null,
> location varchar(40) null,
> constraint asset_number_bak_tmp_pk primary key clustered (asset_number))
>
>When I run the procedure, I get this message:
>
>(1 row(s) affected)
>Msg 2714, Level 16, State 4, Procedure updatenavharrierdb, Line 19
>There is already an object named 'asset_number_bak_tmp_pk' in the database.
>Msg 1750, Level 16, State 0, Procedure updatenavharrierdb, Line 19
>Could not create constraint. See previous errors.
>
>How can I find where else the system thinks this constraint exists?
>
>
>I tried this but it only finds it in one place (one row in the result set),
>i.e. my procedure:
>
>select sysobjects.name, syscomments.text
>from sysobjects, syscomments
>where sysobjects.id = syscomments.id and
>((lower(sysobjects.name) like '%asset_number_bak_tmp_pk%') or
> (lower(syscomments.text) like '%asset_number_bak_tmp_pk%'))
>
>Is this somehow a case where I need to do something dynamically, or purge some
>information? I thought temp tables and their crony constraints disappeared
>after the procedure exited.
>
>thanks
>Jeff Kish

I got around the problem by removing the 'constraint name' clause.
I guess it is optional, but I still wonder is it 'wrong' to have a named
constraint on a temporary table, and shouldn't it of gone away with the table
at the end of procedure execution?

thanks for the illumination, shots, etc.
Jeff Kish
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 03:05 PM
Erland Sommarskog
 
Posts: n/a
Default Re: create constraint error

Jeff Kish (jeff.kish@mro.com) writes:
> I got around the problem by removing the 'constraint name' clause. I
> guess it is optional, but I still wonder is it 'wrong' to have a named
> constraint on a temporary table, and shouldn't it of gone away with the
> table at the end of procedure execution?


Correct. For regular tables it is good practice to explicitly name your
constraints. But for temp tables you should never do it. The point with
temp tables is that two different sessions can create the same table without
conflict, because the real name behind the scenes is padded with some stuff
to make it unique. But if you name the constraints you lose this.

Keep in mind that all temp tables end up in sys.objects, and this table
(well view) also includes names of constraints.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:33 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com