sql server 2008 游标循环更新数据

时间:2021-12-21 13:26:54
USE [数据库名称]

DECLARE @Audio_location nvarchar(200)
Declare @unititemID int
declare @newaudio nvarchar(200)

DECLARE My_Cursor CURSOR --定义游标
FOR (select Audio_location,UnititemID from [架构名].[表名]) --查出需要的集合放到游标中
OPEN My_Cursor; --打开游标
FETCH NEXT FROM My_Cursor INTO @Audio_location,@unititemID; --读取第一行数据(将MemberAccount表中的UserId放到@UserId变量中)
WHILE @@FETCH_STATUS = 0
BEGIN

set @newaudio=''




select @newaudio+=a from LCMS.func_split(@Audio_location,'_') where idx=1


select @newaudio+='_'+cast(@unititemID as varchar(100))


select @newaudio+='_'+a from LCMS.func_split(@Audio_location,'_') where idx=3


--select @Audio_location ,@newaudio,@unititemID

UPDATE [架构名].[表名] SET Audio_location = @newaudio WHERE CURRENT OF My_Cursor; --更新

FETCH NEXT FROM My_Cursor INTO @Audio_location,@unititemID; --读取下一行数据

END
CLOSE My_Cursor; --关闭游标
DEALLOCATE My_Cursor; --释放游标
GO