维修单这个表的编号序号应该是1到100的等差数列,但是数据更新错误,出来了好多不等差的数,现在想用游标更新编号序号这列,但是写了下边的游标,数据变成从1开始批量更新,到从2开始批量更新这样的,请问要怎样修改?
declare @id int
declare @sfxm nvarchar(50)
set @id=1
declare cur cursor for
select 编号序号 from 维修单
open cur
Fetch next From Cur Into @sfxm
while @@FETCH_STATUS =0
begin
while @id<(select COUNT(*) from 维修单)
begin
update 维修单 set 编号序号=@id
Fetch next From Cur Into @sfxm
set @id+=1
if(@id>(select COUNT(*) from 维修单))
break
end
Close Cur
Deallocate Cur
end
6 个解决方案
#1
declare @id int declare @sfxm nvarchar(50) set @id=1 declare cur cursor for select 编号序号 from 维修单 open cur Fetch next From Cur Into @sfxm while @@FETCH_STATUS =0 begin while @id<(select COUNT(*) from 维修单) begin with cte as ( select ROW_NUMBER()over(order by 编号序号)id,编号序号 from 维修单 ) update cte set 编号序号=@id where id=@id Fetch next From Cur Into @sfxm set @id+=1 if(@id>(select COUNT(*) from 维修单)) break end Close Cur Deallocate Cur end
#2
你把你的数据贴出来一些看看。
#3
with tb as
(
select *, rownr = ROW_NUmber()over(Partition by ID order by 编号序号)
from 维修单
)
update 维修单
set 编号序号 = rownr
from tb
where 维修单.编号序号 = tb.rownr
#4
维修单这个表的编号序号应该是1到100的等差数列,但是数据更新错误,出来了好多不等差的数,现在想用游标更新编号序号这列,但是写了下边的游标,数据变成从1开始批量更新,到从2开始批量更新这样的,请问要怎样修改?
declare @id int
declare @sfxm nvarchar(50)
set @id=1
declare cur cursor for
select 编号序号 from 维修单
open cur
Fetch next From Cur Into @sfxm
while @@FETCH_STATUS =0
begin
while @id<(select COUNT(*) from 维修单)
begin
update 维修单 set 编号序号=@id
Fetch next From Cur Into @sfxm
set @id+=1
if(@id>(select COUNT(*) from 维修单))
break
end
Close Cur
Deallocate Cur
end
刚开始没看懂题。
如果只是想添加个int型流水号,试试这个。
with tb as
(
select *, rownr = ROW_NUmber()over(order by 维修id)
from 维修单
)
update 维修单
set 编号序号 = rownr
from tb
where 维修单.维修id = tb.维修id
#6
try this,
update a set a.编号序号=b.rn from 维修单 a inner join (select 编号序号, row_number() over(order by 编号序号) 'rn' from 维修单) b on a.编号序号=b.编号序号
#1
declare @id int declare @sfxm nvarchar(50) set @id=1 declare cur cursor for select 编号序号 from 维修单 open cur Fetch next From Cur Into @sfxm while @@FETCH_STATUS =0 begin while @id<(select COUNT(*) from 维修单) begin with cte as ( select ROW_NUMBER()over(order by 编号序号)id,编号序号 from 维修单 ) update cte set 编号序号=@id where id=@id Fetch next From Cur Into @sfxm set @id+=1 if(@id>(select COUNT(*) from 维修单)) break end Close Cur Deallocate Cur end
#2
维修单这个表的编号序号应该是1到100的等差数列,但是数据更新错误,出来了好多不等差的数,现在想用游标更新编号序号这列,但是写了下边的游标,数据变成从1开始批量更新,到从2开始批量更新这样的,请问要怎样修改?
declare @id int
declare @sfxm nvarchar(50)
set @id=1
declare cur cursor for
select 编号序号 from 维修单
open cur
Fetch next From Cur Into @sfxm
while @@FETCH_STATUS =0
begin
while @id<(select COUNT(*) from 维修单)
begin
update 维修单 set 编号序号=@id
Fetch next From Cur Into @sfxm
set @id+=1
if(@id>(select COUNT(*) from 维修单))
break
end
Close Cur
Deallocate Cur
end
你把你的数据贴出来一些看看。
#3
with tb as
(
select *, rownr = ROW_NUmber()over(Partition by ID order by 编号序号)
from 维修单
)
update 维修单
set 编号序号 = rownr
from tb
where 维修单.编号序号 = tb.rownr
#4
维修单这个表的编号序号应该是1到100的等差数列,但是数据更新错误,出来了好多不等差的数,现在想用游标更新编号序号这列,但是写了下边的游标,数据变成从1开始批量更新,到从2开始批量更新这样的,请问要怎样修改?
declare @id int
declare @sfxm nvarchar(50)
set @id=1
declare cur cursor for
select 编号序号 from 维修单
open cur
Fetch next From Cur Into @sfxm
while @@FETCH_STATUS =0
begin
while @id<(select COUNT(*) from 维修单)
begin
update 维修单 set 编号序号=@id
Fetch next From Cur Into @sfxm
set @id+=1
if(@id>(select COUNT(*) from 维修单))
break
end
Close Cur
Deallocate Cur
end
你把你的数据贴出来一些看看。
维修单这个表的编号序号应该是1到100的等差数列,但是数据更新错误,出来了好多不等差的数,现在想用游标更新编号序号这列,但是写了下边的游标,数据变成从1开始批量更新,到从2开始批量更新这样的,请问要怎样修改?
declare @id int
declare @sfxm nvarchar(50)
set @id=1
declare cur cursor for
select 编号序号 from 维修单
open cur
Fetch next From Cur Into @sfxm
while @@FETCH_STATUS =0
begin
while @id<(select COUNT(*) from 维修单)
begin
update 维修单 set 编号序号=@id
Fetch next From Cur Into @sfxm
set @id+=1
if(@id>(select COUNT(*) from 维修单))
break
end
Close Cur
Deallocate Cur
end
刚开始没看懂题。
如果只是想添加个int型流水号,试试这个。
with tb as
(
select *, rownr = ROW_NUmber()over(order by 维修id)
from 维修单
)
update 维修单
set 编号序号 = rownr
from tb
where 维修单.维修id = tb.维修id
#6
try this,
update a set a.编号序号=b.rn from 维修单 a inner join (select 编号序号, row_number() over(order by 编号序号) 'rn' from 维修单) b on a.编号序号=b.编号序号