首页 > 代码库 > sql server 更新表,每天的数据分固定批次设置批次号sql

sql server 更新表,每天的数据分固定批次设置批次号sql

按表中的字段 UpdateTime 按每天进行编号,每天的编号都从1开始编号,并附带表的主键 cid,把数据存入临时表中

WITH temp AS
(
SELECT cid,updatetime,

ROW_NUMBER() OVER (PARTITION BY CONVERT(varchar(10), UpdateTime, 20) ORDER BY updatetime) AS Row_Num

FROM Customer_Transfer
)

从临时表中根据主键cid进行更新 BatchNo,给予编号设置

UPDATE m SET m.BatchNo = t.Row_Num
FROM Customer_Transfer m JOIN temp t
ON m.cid = t.cid

按一个批次号100条的规则对每天的批次号进行重新编号

UPDATE Customer_Transfer SET  BatchNo = (BatchNo/100 + 1)