将Timestamp列添加到密钥并消除重复项

时间:2015-07-29 14:13:28

标签: sql sql-server

我需要将Timestamp列添加到包含多个列的键约束中。

此列在时间戳上没有毫秒部分。 Timestamp列可能存在同一时间的问题。我尝试通过添加一些随机的毫秒部分来消除它们。

我的查询:

update [vM].[dbo].[Operator] 
set [vM].[dbo].[Operator].Timestamp = DATEADD(MS , A.RowNumber*3 % 1000 , A.Timestamp),
    OperatorID = A.OperatorID ,
    ActionID = A.ActionID ,
    McID = A.McID ,
    Station = A.Station ,
    Slot = A.Slot ,
    SubSlot = A.SubSlot,
    FeederID = A.FeederID,
    CompID = A.CompID,
    Description = A.Description
from
 (SELECT (ROW_NUMBER() OVER (Order by timestamp)) % 1000  as RowNumber , *
  FROM [vM].[dbo].[Operator]
  WHERE DATEPART(ms,Timestamp) % 1000 = 0) as A

我收到错误The duplicate key value is (Dec 7 2014 10:54AM, ??, 28, 23).这是我尝试更新的原始记录。

我的原创时间是2014-12-07 10:54:52.000

我的查询有什么问题?是否有更主持的方式来实现我想要做的事情?

1 个答案:

答案 0 :(得分:1)

如果您的值少于1000且时间戳相等

,请尝试使用此值
update [vM].[dbo].[Operator] 
set [vM].[dbo].[Operator].Timestamp = DATEADD(MS , A.RowNumber , A.Timestamp),
    OperatorID = A.OperatorID ,
    ActionID = A.ActionID ,
    McID = A.McID ,
    Station = A.Station ,
    Slot = A.Slot ,
    SubSlot = A.SubSlot,
    FeederID = A.FeederID,
    CompID = A.CompID,
    Description = A.Description
from
 (SELECT (ROW_NUMBER() OVER (partition by timestamp Order by timestamp)) as RowNumber , *
  FROM [vM].[dbo].[Operator]
  WHERE DATEPART(ms,Timestamp) % 1000 = 0) as A

当您达到较大的RowNumber值时可能会出现问题