Solved

Check if table exist before creating a constraint.

Posted on 2007-03-29
5
496 Views
Last Modified: 2012-06-27
ALTER TABLE #Tmp_LN_Discount ADD
            CONSTRAINT PK_Tmp_LN_Discount PRIMARY KEY NONCLUSTERED
            (      STR_ID,
                  RGST_ID,
                  TRAN_ID,
                  TRAN_LN_NUM,
                  DISC_CD            )  ON [PRIMARY]

Check if table exist before creating a constraint.

There is already an object named 'PK_Tmp_LN_Discount' in the database.
0
Comment
Question by:mathieu_cupryk
  • 3
5 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 18819057
if exists (select name from sysobjects where name like '#tmp_ln_discount%')
begin
     drop table #tmp+ln_discount
end
create table...
add constraint...

0
 
LVL 16

Expert Comment

by:rboyd56
ID: 18819082
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[#Tmp_LN_Discount]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
ALTER TABLE #Tmp_LN_Discount ADD
            CONSTRAINT PK_Tmp_LN_Discount PRIMARY KEY NONCLUSTERED
            (      STR_ID,
                  RGST_ID,
                  TRAN_ID,
                  TRAN_LN_NUM,
                  DISC_CD            )  ON [PRIMARY]
GO
0
 
LVL 16

Accepted Solution

by:
rboyd56 earned 500 total points
ID: 18819100
You might not want to drop the table first. It may contain data.

This is more correct. My cut and past went crazy:


if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[#Tmp_LN_Discount]')
ALTER TABLE #Tmp_LN_Discount ADD
            CONSTRAINT PK_Tmp_LN_Discount PRIMARY KEY NONCLUSTERED
            (      STR_ID,
                  RGST_ID,
                  TRAN_ID,
                  TRAN_LN_NUM,
                  DISC_CD            )  ON [PRIMARY]
GO
0
 

Author Comment

by:mathieu_cupryk
ID: 18819164
rboy
what about CREATE TABLE #Tmp_LN_Discount (STR_ID INT NOT NULL, RGST_ID INT NOT NULL, TRAN_ID INT NOT NULL, TRAN_LN_NUM SMALLINT NOT NULL, DISC_CD INT NOT NULL, DISC_AMT MONEY NULL)
      
There is already an object named '#Tmp_LN_Discount' in the database.
0
 
LVL 16

Expert Comment

by:rboyd56
ID: 18819224
I was looking only at the contraint portion of the command. You can do the same type of query:

if exists (select name from sysobjects where name like '#tmp_ln_discount%')
begin
     drop table #tmp+ln_discount
end

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question