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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM