批量修改SQLServer數據庫表字段屬性


以下提供一些場景,各位舉一反三自行發揮

修改所有varchar為nvarchar,同時保持字段長度一致

declare c_sql cursor for
      SELECT sql = 'alter table [' + d.name + '] alter column [' + a.name +
       '] nvarchar('+Convert(varchar,a.length)+')' --** 修改為什么屬性
  FROM syscolumns a
  left join systypes b
    on a.xtype = b.xusertype
 inner join sysobjects d
    on a.id = d.id
   and d.xtype = 'U'
   and d.name <> 'dtproperties'
 where b.name = 'varchar'
   and not exists
 (SELECT 1
          FROM sysobjects
         where xtype = 'PK'
           and name in (SELECT name
                          FROM sysindexes
                         WHERE indid in (SELECT indid
                                           FROM sysindexkeys
                                          WHERE id = a.id
                                            AND colid = a.colid))) --** 排除主鍵修改
 order by d.name, a.name
 
 
declare @sql varchar(1000)
     open c_sql
     fetch next from c_sql into @sql
     while @@fetch_status = 0
     begin
     --select @sql
        exec(@sql)
     fetch next from c_sql into @sql
     end
     close c_sql
     deallocate c_sql 

修改所有字段smalldatetime為datetime類型且非空

declare c_sql cursor for
      SELECT sql = 'alter table [' + d.name + '] alter column [' + a.name +
       '] datetime not null' --** 修改為什么屬性
  FROM syscolumns a
  left join systypes b
    on a.xtype = b.xusertype
 inner join sysobjects d
    on a.id = d.id
   and d.xtype = 'U'
   and d.name <> 'dtproperties'
 where b.name = 'smalldatetime'
   and not exists
 (SELECT 1
          FROM sysobjects
         where xtype = 'PK'
           and name in (SELECT name
                          FROM sysindexes
                         WHERE indid in (SELECT indid
                                           FROM sysindexkeys
                                          WHERE id = a.id
                                            AND colid = a.colid))) --** 排除主鍵修改
 order by d.name, a.name
 
 
declare @sql varchar(1000)
     open c_sql
     fetch next from c_sql into @sql
     while @@fetch_status = 0
     begin
     --select @sql
        exec(@sql)
     fetch next from c_sql into @sql
     end
     close c_sql
     deallocate c_sql 

 

**給所有字段添加默認值,以varchar為例:

 

declare c_sql cursor for
      SELECT sql = 'ALTER TABLE '+d.name+' ADD  CONSTRAINT [DF_'+d.name+'_'+a.name+']  DEFAULT ('''') FOR ['+a.name+']' --** 修改為什么屬性
  FROM syscolumns a
  left join systypes b
    on a.xtype = b.xusertype
 inner join sysobjects d
    on a.id = d.id
   and d.xtype = 'U'
   and d.name <> 'dtproperties'
 where b.name = 'nvarchar'
   and not exists
 (SELECT 1
          FROM sysobjects
         where xtype = 'PK'
           and name in (SELECT name
                          FROM sysindexes
                         WHERE indid in (SELECT indid
                                           FROM sysindexkeys
                                          WHERE id = a.id
                                            AND colid = a.colid))) --** 排除主鍵修改
 order by d.name, a.name
 
 
declare @sql varchar(1000)
     open c_sql
     fetch next from c_sql into @sql
     while @@fetch_status = 0
     begin
     --select @sql
        exec(@sql)
     fetch next from c_sql into @sql
     end
     close c_sql
     deallocate c_sql

 


免責聲明!

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



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