游标虽然效率不高,但是在工作中,我们也会使用到它,在这里记下游标模板,供以后使用。
begin
declare @columnA nvarchar(max)
declare @ID int
declare order_cursor cursor
for (select ID from table where column='value') --modify row
open order_cursor
fetch next from order_cursor into @ID
while @@FETCH_STATUS = 0
begin
--PRINT @tezmp
--[Start][do some thing begin] -
select @columnA = (select top(1) columnA from table where ID = @ID ) -modify row
update table set columnA = @columnA where iofID = @tezmp -modify row
--[End][do some thing begin]
fetch next from order_cursor into @tezmp
end
close order_cursor
deallocate order_cursor
end
go
只需要修改其中一部分,就能达到效果, flag is “modify row”