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.
Author |
Topic |
sunny_10
Yak Posting Veteran
72 Posts |
Posted - 2013-03-20 : 00:29:17
|
HiI 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 |
 |
|
djj55
Constraint Violating Yak Guru
352 Posts |
Posted - 2013-03-20 : 13:39:15
|
Could also useIF 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 |
 |
|
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 |
 |
|
|
|
|