declare @user_name varchar(100)
--定义一个游标
declare user_cur cursor for select user_name from [T_CORE_USER] where Bh is null
--打开游标
open user_cur
while @@fetch_status=0
begin
--读取游标
fetch next from user_cur into @user_name
begin
if (select count(*) from T_smes_qyxx where qy_mc = @user_name )<=1
update t1
set t1.BH = t2.QY_ID from [T_CORE_USER] t1,T_smes_qyxx t2
where t1.user_name = @user_name and t2.qy_mc = @user_name
and t1.Bh is null
print @user_name
end
end
close user_cur
--摧毁游标
deallocate user_cur
SQLSERVER游标的使用
最新推荐文章于 2025-07-24 15:24:32 发布
本文介绍了一个使用SQL游标进行数据检索及条件更新的例子。通过定义游标、打开和遍历游标来更新两个表中匹配项的BH字段,并确保每个用户名对应的记录不超过一条。
281

被折叠的 条评论
为什么被折叠?



