批量修改Ms SqlServer 的default(默认值)

王朝学院·作者佚名  2009-10-25  
宽屏版  字体: |||超大  

1、取得数据库所有表的默认值:

select

t3.name as 表名,t1.name as 字段名,t2.text as 默认值 ,t4.name

from syscolumns t1,syscomments t2,sysobjects t3 ,sysobjects t4

where t1.cdefault=t2.id and t3.xtype='u' and t3.id=t1.id

and t4.xtype='d' and t4.id=t2.id;

2、生成删除所有默认值的语句:

select "ALTER TABLE " + t3.name + " DROP CONSTRAINT " +t4.name +";"

from syscolumns t1,syscomments t2,sysobjects t3 ,sysobjects t4

where t1.cdefault=t2.id and t3.xtype='u' and t3.id=t1.id

and t4.xtype='d' and t4.id=t2.id ;

3、生成批量添加所有默认值的语句:

select "alter table " + t3.name + " add default (1) for " +t1.name +";"

from syscolumns t1,syscomments t2,sysobjects t3 ,sysobjects t4

where t1.cdefault=t2.id and t3.xtype='u' and t3.id=t1.id

and t4.xtype='d' and t4.id=t2.id ;

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有