Solved

Check if table exist before creating a constraint.

Posted on 2007-03-29
5
463 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now