zoukankan      html  css  js  c++  java
  • 批量修改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
  • 相关阅读:
    意外发现在调用Activator.CreateInstance的时候在构造函数处加断点居然可以~~
    手机操作系统
    读取Excel文件到DataSet
    支持mrp软件的手机(MTK手机)检测
    如何查看手机系统版本
    .NET进度条用例
    dos命令导出指定类型的文件列表
    FTP上传下载 FTP操作类 FTPHelper 异步上传 递归创建文件文件夹
    批量删除GridView(DataGrid)选中项
    sql判断临时表是否存在
  • 原文地址:https://www.cnblogs.com/cdoneiX/p/12258506.html
Copyright © 2011-2022 走看看