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

  • 相关阅读:
    N-Queens II
    N-Queens
    Insertion Sort List
    Combination Sum
    Next Permutation
    Permutations II
    Unique Paths II
    【转】Python之mmap内存映射模块(大文本处理)说明
    【转】python之模块array
    【转】python 退出程序的方式
  • 原文地址:https://www.cnblogs.com/homeym/p/13372926.html
Copyright © 2011-2022 走看看