34,590
社区成员
发帖
与我相关
我的任务
分享
begin tran
DECLARE @mark INT
SELECT @mark=MAX(MARK) from emBaseInf
;with a1 as
(
select mark,row_number() over (order by @@servername) re from emBaseInf where Mark=''
)
update a1 set mark=right('000'+rtrim(re+@mark),4)
commit tran
--drop table tb
create table tb(ID int, mark varchar(100))
insert into tb
select 1,null union all
select 2,'a' union all
select 3,null union all
select 4,'b' union all
select 5,'b' union all
select 6,null
go
declare @t int
set @t = 0
update tb
set @t = @t + 1,
mark = case when mark is null then right('000'+cast(@t%10000 as varchar),4)
else mark end
select *
from tb
/*
ID mark
1 0001
2 a
3 0003
4 b
5 b
6 0006
*/
create table tb(ID int, mark varchar(100))
insert into tb
select 1,null union all
select 2,'a' union all
select 3,null union all
select 4,'b' union all
select 5,'b' union all
select 6,null
go
declare @t int
set @t = 0
update tb
set @t = @t + 1,
mark = case when mark is null then right('000'+cast(@t as varchar),4)
else mark end
select *
from tb
/*
ID mark
1 0001
2 a
3 0003
4 b
5 b
6 0006
*/