Sql Server 默认值

--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;

  --取得数据库中已有默认值的脚本
select  'ALTER TABLE [' +  t3.name  + '] ADD DEFAULT ' + t2.text + ' 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 ORDER BY t3.name, t1.name;

--alter table MES_MOAppoint_ProdProcSteps  add default  (1)  for ContainerChangeOut;


--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 ORDER BY t3.name, t1.name;


--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 ;
View Code

 

转载于:https://www.cnblogs.com/kenchan/p/7898675.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值