SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Primary Index on 2 fields
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sunny_10
Yak Posting Veteran

63 Posts

Posted - 03/20/2013 :  00:29:17  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2168 Posts

Posted - 03/20/2013 :  01:04:59  Show Profile  Reply with Quote
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

USA
283 Posts

Posted - 03/20/2013 :  13:39:15  Show Profile  Reply with Quote
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

USA
769 Posts

Posted - 03/20/2013 :  15:53:25  Show Profile  Reply with Quote
Make sure you include the object type if using object_id...

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000