【发布时间】:2019-08-28 14:06:02
【问题描述】:
我有一个表,我想使用 T-SQL Merge statement 为其创建 SCD Type 2,但它没有唯一键。
RoleTaskTable:
RoleID, TaskID
1,A
1,B
1,C
2,A
2,D
2,F
3,A
3,B
3,E
3,F
显然我得到了错误
"The MERGE statement attempted to UPDATE or DELETE the same row more than once. This happens when a target row matches more than one source row. A MERGE statement cannot UPDATE/DELETE the same row of the target table multiple times. Refine the ON clause to ensure a target row matches at most one source row, or use the GROUP BY clause to group the source rows."
当我将 RoleID 和 TaskID 组合为暂存表和 SCDTable 的唯一索引时,它会简单地将其识别为新记录,因此所有记录(即使某些记录已被删除)仍标记为在 SCD 表中处于活动状态。
你怎么能解决这样的问题?
我可以展示我为这个 SCD 拥有的整个代码,但我想我在这里错过了一些基本的东西。
【问题讨论】:
标签: sql-server tsql sql-server-2012 scd