for i=0 to 4 update table set col_b=i+1 where col_a=a next
有没有不用循环的方法?直接UPDATE?
declare i int set @i = 0update 表 set @i=@i+1,col_b=@i
--生成测试数据 create table #t(col_a varchar(10),col_b int) insert into #t select 'a',null insert into #t select 'a',null insert into #t select 'a',null insert into #t select 'a',null insert into #t select 'a',null--执行更新操作 declare @col_a varchar(10),@i int set @i = 0 update #t set @i=@i+1,col_b=@i--查看更新结果 select * from #t/* col_a col_b ----- ----- a 1 a 2 a 3 a 4 a 5 */
declare @aa int set @aa =1 update a set @aa=col_b =@aa+1
update table set col_b=i+1 where col_a=a
next
set @i = 0update 表 set @i=@i+1,col_b=@i
create table #t(col_a varchar(10),col_b int)
insert into #t select 'a',null
insert into #t select 'a',null
insert into #t select 'a',null
insert into #t select 'a',null
insert into #t select 'a',null--执行更新操作
declare @col_a varchar(10),@i int
set @i = 0
update #t set @i=@i+1,col_b=@i--查看更新结果
select * from #t/*
col_a col_b
----- -----
a 1
a 2
a 3
a 4
a 5
*/
set @aa =1
update a
set @aa=col_b =@aa+1