-- 危险操作,处理前记得先备份数据库 1 declare @sql varchar(500),@tbname varchar(100) 2 begin 3 4 -- 创建游标 5 declare cursor
-- 危险操作,处理前记得先备份数据库
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 表示查询非自增长标识列
--结束END--
本文标题: MS-SQLSERVER 批量修改表中 某个字段可为NULL
本文链接: https://www.lsjlt.com/news/7470.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
2024-04-28
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0