sql server 判断及增加列的默认值约束

IF NOT EXISTS ( SELECT name FROM sysobjects WHERE id = ( SELECT syscolumns.cdefault FROM sysobjects 
    INNER JOIN syscolumns ON sysobjects.Id=syscolumns.Id 
    WHERE sysobjects.name=NtableName AND syscolumns.name=NcolumnName ) 
)
BEGIN    
    ALTER TABLE tableName add constraint DF_columnName DEFAULT NN for columnName;  
    PRINT NNew add DF_columnName;
END
ELSE
BEGIN
    PRINT NDEFAULT FOR columnName HAD EXISTS;
END

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。