开发者

how to check and alter constraints

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 compac开发者_开发百科t(.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..


 IF NOT EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'PRIMARY KEY') AND type in (N'U') )
   BEGIN 
       alter table [tablename] add constraint [PRIMARY KEY] PRIMARY KEY CLUSTERED (columnname)
   END

Please use the above query and let me know if it works. thanks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜