SQL SERVER循环查询更新

USE E_MainBoard
GO
DECLARE My_Cursor CURSOR --定义游标
FOR(select * from MainBoardData where SN LIKE 'ZF2145%' and (Date_Time like '2021-10%' or Date_Time LIKE '2021-11-01%'))
OPEN My_Cursor; --打开游标
fetch next from My_Cursor; --读取第一行数据
while @@FETCH_STATUS=0
	begin
		--update MainBoardData set SN=SN+'old' where current of My_Cursor; --更新
		update MainBoardData set SN=REPLACE(SN,'oldoldoldoldoldoldoldoldoldoldoldold','_1') where current of My_Cursor; --更新
		--DELETE FROM 表名 WHERE CURRENT OF My_Cursor; --删除
		fetch next from My_Cursor; --读取下一行数据
	end
close My_Cursor; --关闭游标
deallocate My_Cursor; --释放游标
go

你可能感兴趣的:(SQL,sql,sqlserver,数据库)