declare @csname varchar(100) declare @tablename varchar(100) declare @coulname varchar(100) set @tablename='KY_XMLX_TRANS' set @coulname='sl' --select c.name from sysconstraints a inner join syscolumns b on a.colid=b.colid --inner join sysobjects c on a.constid=c.id --where a.id=object_id(@tablename) --and b.name=@coulname select @csname=[name] from sysobjects t where id=(select cdefault from syscolumns where id=object_id(N''+@tablename+'') and name=@coulname) exec('alter table '+@tablename+' drop constraint '+@csname) print(@tablename) print(@coulname) print(@csname) exec ('ALTER TABLE '+@tablename+' add constraint ' + @csname + ' DEFAULT (0) FOR '+@coulname+'')
1):已存在默认值
语法:exec sp_helpconstraint @objname=表名 (查看指定表相关约束详情)
alter table 表名 drop constraint 约束名 (删除默认值)
alter table 表名 add default 默认值 for 字段名
2:):无默认值
语法:alter table 表名 add default 默认值 for 字段名