当前位置: 代码迷 >> Sql Server >> 想把数据库所有表中数据类型为 numeric 的列设一个默认值0,要怎么做
  详细解决方案

想把数据库所有表中数据类型为 numeric 的列设一个默认值0,要怎么做

热度:343   发布时间:2016-04-27 12:52:41.0
想把数据库所有表中数据类型为 numeric 的列设一个默认值0,要如何做?
如题

------解决方案--------------------
SQL code
select 'alter table '+OBJECT_NAME(object_id)+ ' ADD CONSTRAINT col_'+ name +'DEFAULT 0 FOR '+name+';'from sys.columns where system_type_id = 108
------解决方案--------------------
SQL code
执行下面语句后,把结果拷贝到分析器,再执行就可以.select 'alter table '+OBJECT_NAME(object_id)+ ' ADD CONSTRAINT col_'+ name +'DEFAULT 0 FOR '+name+';'from sys.columns where system_type_id = 108
------解决方案--------------------

select 'alter table '+object_Name(a.id) + ' add constraint DF_'+a.name+' default 0 for '+a.name
from sysColumns a
inner join systypes b on a.xusertype=b.xusertype
where b.name='numeric'
  相关解决方案