views:

26

answers:

0

hi can anyone tel me how to check whether a primary key exists or not in a table and add a primary key if not exixts in sql server compact(.sdf)..

i'm using this,

  IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_TYPE = 'PRIMARY KEY') 
   BEGIN 
       alter table [tablename] add constraint [name] PRIMARY KEY (columnname)
   END

when i execute this in sql server compact i get this error..

Major Error 0x80040E14, Minor Error 25501

IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_TYPE = 'PRIMARY KEY') BEGIN alter table [tablename] add constraint [name] PRIMARY KEY (columnname) END There was an error parsing the query. [ Token line number = 1,Token line offset = 1,Token in error = IF ]

thank you..