我想知道是否可以在 SQL 中执行以下操作(我使用的是 SQL Server 2008+,因此特定于供应商的东西暂时也可能有用):
我有下表:
contact (contactid, fullname, fullname_timestamp, age, age_timestamp)
我只想更新那些“*_timestamp”列小于给定值的列。例如:
UPDATE contact
SET fullname = 'john doe' IF fullname_timestamp < 12345,
age = 30 IF age_timestamp < 12345
WHERE contactid = 'abcde'
将这些条件放在 WHERE 子句中会产生“全有或全无”的场景(我认为?),而鉴于这些“时间戳”约束,我想“部分更新”记录。
这可能/我做错了吗?欢迎提出建议。提前致谢。
编辑:@AaronBertrand 提出了一些好的观点,我认为它们值得一提作为问题的附加信息。我还要承认,我仍在脑海中想出一个解决方案,而且我的想法可能存在差距。
我正在复制外部产品中包含的数据。该产品具有明确定义的 CRUD 事件管道。由于性能原因,我正在异步管理这些事件。这些事件可能会乱序排队以供以后处理。我在上述产品的非异步管道阶段之一中使用 DateTime.UtcNow.Ticks 时间戳 - 以便记录正确加时间戳 - 我有办法判断我要更新的数据是否是最新的-日期或陈旧。
-- sync pipeline --
record update 1 - timestamp 1
record update 2 - timestamp 2
-- async queue (can be out-of-order) --
record update 2 - timestamp 2 -> sent to db
record update 1 - timestamp 1 -> sent to db
-- replica db --
record update 2 goes through, the timestamp is ok
record update 1 does nothing, timestamp shows data is out-of-date
是的,更新可以同时发生在相同的记录上。用户可以同时修改相同或不同的字段。希望这是有道理的。感谢大家的大力投入。