日期:2014-05-18 浏览次数:20469 次
DECLARE @i INT,@A VARCHAR(10) SET @i=100 UPDATE tableName SET B=CASE WHEN A=@A THEN A+RIGHT(@i+1,2) ELSE A+'01' END,@i=CASE WHEN A=@A THEN @i+1 ELSE 1 END,@A=A
------解决方案--------------------
go if OBJECT_ID('tbl')is not null drop table tbl go create table tbl( [Message] int, ServiceRequestId int ) go insert tbl select 3905,170757 union all select 3905,173116 union all select 4221,173123 union all select 4221,173127 union all select 4221,173132 -- 查询处理 --随便选取了你的结果中的两条数据: /* Message ServiceRequestId 3905 170757,173116 4221 173123,173127,173132 */ --> 测试数据:[tblA] if object_id('[tbl]') is not null drop table [tbl] create table [tbl](A varchar(4),B VARCHAR(10)) insert [tbl] select '0001',null union all select '0001',null union all select '0001',null union all select '0001',null union all select '0002',null union all select '0002',null union all select '0002',null union all select '0003',null union all select '0003',null ;with t as( select ROW_NUMBER()over(PARTITION by A order by getdate()) as id, * from tbl ) UPDATE tbl SET B=T.A+RIGHT('00'+LTRIM(ID),2) FROM T WHERE T.A=tbl.A