给表增加主键,先判断表是否存在主键
给表增加主键,同时需要判断该表是否存在主键,也需要处理将该字段先处理为不为null。如下处理:
1 GO 2 IF NOT EXISTS(select 1 from sysobjects where xtype= 'PK' and parent_obj = object_id('cb_Contract')) 3 BEGIN 4 alter table cb_Contract alter column ContractGUID UNIQUEIDENTIFIER NOT NULL; 5 END 6 GO 7 IF NOT EXISTS(select 1 from sysobjects where xtype= 'PK' and parent_obj = object_id('cb_Contract')) 8 BEGIN 9 alter table cb_Contract add primary key (ContractGUID); 10 END 11 GO