给表增加主键,先判断表是否存在主键

给表增加主键,同时需要判断该表是否存在主键,也需要处理将该字段先处理为不为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
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-27
  • 2021-07-05
  • 2021-12-29
  • 2021-12-15
  • 2022-01-04
猜你喜欢
  • 2021-12-29
  • 2021-10-08
  • 2021-08-30
  • 2022-12-23
  • 2021-10-03
  • 2022-03-08
  • 2022-12-23
相关资源
相似解决方案