INSERT/UPDATE 违反 PK 约束
我有一个像这样的维度表:
my_Table
pk1 Primary key
pk2 Primary key
pk3 Primary key
col1
col2
col3
...
并使用一个过程用 MERGE INTO 语句填充该表:
MERGE INTO my_Table dest
USING
( SELECT <columns>
FROM <tables>
WHERE <conditions> ) src
ON
(dest.pk1 = src.pk1 AND dest.pk2 = src.pk2 AND pk3 = src.pk3)
WHEN MATCHED THEN UPDATE SET dest.col1 = src.col1 ,
dest.col2 = src.col2 ,
dest.col3 = src.col3
WHEN NOT MATCHED THEN INSERT (pk1, pk2, pk3, col1, col2, col3)
VALUES (src.pk1, src.pk2, src.pk3, src.col1, src.col2, src.col3);
我的问题是查询“src' 返回一些具有相同主键的行,但它们不是重复的行,例如
| pk1 | pk2 | pk3 | col1 | col2 | col3 |
--------------------------------------------------
| 100 | abc | x99 | 6000,00 | 01/01/2010 | 50,00 |
| 100 | abc | x99 | 0,00 | 01/01/2010 | 30,00 |
| 110 | rty | b50 | 345,00 | 08/10/2009 | 10,00 |
| 120 | xyz | y91 | 1200,00 | 13/02/2009 | 12,50 |
| 120 | xyz | y91 | 1200,00 | 13/02/2009 | 0,00 |
| 120 | xyz | y91 | 1200,00 | 13/02/2009 | 0,00 |
| 120 | xyz | y91 | 1200,00 | 13/02/2009 | 0,00 |
因此,当我调用我的过程时,它返回违反唯一约束的错误。
这个问题的解决办法是什么?顺便说一下,我不是查询src
的编写者...
如果我需要修改src
,我应该有pk (100,abc,x99) :
| pk1 | pk2 | pk3 | col1 | col2 | col3 |
--------------------------------------------------
| 100 | abc | x99 | 6000,00 | 01/01/2010 | 80,00 |
对于 pk (120,xyz,y91) 我会:
| pk1 | pk2 | pk3 | col1 | col2 | col3 |
--------------------------------------------------
| 120 | xyz | y91 | 1200,00 | 13/02/2009 | 12,50 |
提前感谢您的任何建议。
i have a Dimension Table like this :
my_Table
pk1 Primary key
pk2 Primary key
pk3 Primary key
col1
col2
col3
...
and using a procedure to fill this table with the MERGE INTO statement :
MERGE INTO my_Table dest
USING
( SELECT <columns>
FROM <tables>
WHERE <conditions> ) src
ON
(dest.pk1 = src.pk1 AND dest.pk2 = src.pk2 AND pk3 = src.pk3)
WHEN MATCHED THEN UPDATE SET dest.col1 = src.col1 ,
dest.col2 = src.col2 ,
dest.col3 = src.col3
WHEN NOT MATCHED THEN INSERT (pk1, pk2, pk3, col1, col2, col3)
VALUES (src.pk1, src.pk2, src.pk3, src.col1, src.col2, src.col3);
my problem is that the query 'src' returns some rows with same Primary keys, but they are no duplicated rows, example
| pk1 | pk2 | pk3 | col1 | col2 | col3 |
--------------------------------------------------
| 100 | abc | x99 | 6000,00 | 01/01/2010 | 50,00 |
| 100 | abc | x99 | 0,00 | 01/01/2010 | 30,00 |
| 110 | rty | b50 | 345,00 | 08/10/2009 | 10,00 |
| 120 | xyz | y91 | 1200,00 | 13/02/2009 | 12,50 |
| 120 | xyz | y91 | 1200,00 | 13/02/2009 | 0,00 |
| 120 | xyz | y91 | 1200,00 | 13/02/2009 | 0,00 |
| 120 | xyz | y91 | 1200,00 | 13/02/2009 | 0,00 |
So, when i call my procedure it returns the unique constraint violated error.
what could be the solution of this problem? im not the writer of the query src
by the way ...
if i need to modify src
i should have for the pk (100,abc,x99) :
| pk1 | pk2 | pk3 | col1 | col2 | col3 |
--------------------------------------------------
| 100 | abc | x99 | 6000,00 | 01/01/2010 | 80,00 |
and for the pk (120,xyz,y91) i would have :
| pk1 | pk2 | pk3 | col1 | col2 | col3 |
--------------------------------------------------
| 120 | xyz | y91 | 1200,00 | 13/02/2009 | 12,50 |
Thanks in advance for any suggestion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显然,必须对 src 进行一些操作才能使其返回唯一的主键。如果您不太关心表中数据的质量,您可以使用聚合并将其更改为例如
但您可能需要更复杂的处理才能理解这些内容。
编辑:将第一个
sum
更改为max
,以反映问题中的编辑It's obvious that something has to be done with
src
to make it return unique primary keys. If you don't care too much about the quality of the data in your table, you could use aggregation and change it e.g. toBut chances are you need more sophisticated processing to make any sense of that stuff.
EDIT: changed first
sum
tomax
, to reflect the edit in the question如果您确实确定不存在主键冲突。请尝试检查 col1、col2 和 col3 是否没有任何唯一约束。也许唯一约束违规并非来自主键字段。
介意与我们分享您的“错误信息”吗?
If you are really sure that there's no primary key conflict. Please try to check whether col1, col2 and col3 doesn't have any unique constraint. Maybe the unique constraint violation doesn't come from primary key field.
Mind to share with us your "error message"?