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  表示查询非自增长标识列

  • 相关阅读:
    Shell命令之文本操作
    乘法表
    万年历
    猜数游戏
    Linux下如何进行FTP安装与设置
    CentOS 安装nload(流量统计)
    linux下创建用户并且限定用户主目录
    ftp 解决不能上传问题
    【题解】[TJOI2018]数学计算
    【平衡树做题记录】
  • 原文地址:https://www.cnblogs.com/homeym/p/13372926.html
Copyright © 2011-2022 走看看