zoukankan      html  css  js  c++  java
  • MS-SQLSERVER 批量修改表中 某个字段可为NULL

    -- 危险操作,处理前记得先备份数据库


    1
    declare @sql varchar(500),@tbname varchar(100) 2 begin 3 4   -- 创建游标 5   declare cursor_item cursor fast_forward for select [name] from sysobjects where xtype='U' AND id in(select id from syscolumns where name='myColumnName' and colstat=0 ) 6   open cursor_item;--打开游标 7   while 1=1 --开始循环 8   begin 9     fetch next from cursor_item into @tbname; --赋值到变量中 10     if(@@fetch_status!=0) break;--如果没有结果退出循环
    11 12     -- 拼接修改字段的SQL语句 13     set @sql = 'alter table '+@tbname+' alter column myColumnName int NULL' 14 15     -- 执行拼接的SQL 16     exec(@sql); 17 18   end 19   close cursor_item --关闭游标 20   deallocate cursor_item 21 22 end;

    注意:

    syscolumns 保存列信息的系统表
    sysobjects 保存表信息的系统表

     colstat=0  表示查询非自增长标识列

  • 相关阅读:
    Codeforces Round #609 (Div. 2)
    Educational Codeforces Round 78 (Rated for Div. 2)
    Codeforces
    crontab
    C6 C7的开机启动流程
    平均负载压力测试
    ps 和 top
    if判断
    使用3种协议搭建本地yum仓库
    linux rpm包
  • 原文地址:https://www.cnblogs.com/homeym/p/13372926.html
Copyright © 2011-2022 走看看