Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Primary Index on 2 fields

Author  Topic 

sunny_10
Yak Posting Veteran

72 Posts

Posted - 2013-03-20 : 00:29:17
Hi

I want Primary Index on 2 fields ShiftCode,EffectiveFrom . Secondly i want if tables exists it should not create table.

sql = " CREATE TABLE Shift" & "" _
& " (shiftcode nvarchar(10) not null, " _
& " CONSTRAINT " & Shift & "_PK6 PRIMARY KEY(shiftcode))"

Thanks

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-03-20 : 01:04:59
IF OBJECT_ID('Shift') IS NULL
CREATE TABLE Shift ( ShiftCode nvarchar(10) NOT NULL, EffectiveFrom DATETIME NOT NULL, CONSTRAINT Shift_PK PRIMARY KEY( ShiftCode, EffectiveFrom))


--
Chandu
Go to Top of Page

djj55
Constraint Violating Yak Guru

352 Posts

Posted - 2013-03-20 : 13:39:15
Could also use
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[shift]') AND type in (N'U'))
CREATE TABLE...
This does what bandi did but is just another way.

djj
Go to Top of Page

jeffw8713
Aged Yak Warrior

819 Posts

Posted - 2013-03-20 : 15:53:25
Make sure you include the object type if using object_id...

IF OBJECT_ID('Shift', 'U') IS NULL

Go to Top of Page
   

- Advertisement -