合并更新oracle无法获得一组稳定的行
我正在尝试根据另一个表(内连接)更新 Oracle 中的一个表,但匹配的字段匹配超过 1 行,因此出现错误:
I am trying to update a table in Oracle based on another table (inner join) but the matching fields match on more than 1 row so I get an error:
无法获得一组稳定的行
MERGE INTO C
USING D
ON (C.SYSTEM = D.SYSTEM)
WHEN MATCHED THEN
UPDATE SET C.REF_CD = D.CODE,
C.REF_DT = TO_DATE('12/05/2017', 'MM/DD/YYYY')
WHERE C.CODE = '123'
AND D.CODE IS NOT NULL
AND C.CLOSED = 'N'
AND C.RCVD_DT >= TO_DATE('12/01/2017', 'MM/DD/YYYY')
AND C.RCVD_DT <= TO_DATE('12/04/2017', 'MM/DD/YYYY')
AND SUBSTR(C.SSN,7,3) >= D.FROM
AND SUBSTR(C.SSN,7,3) <= D.TO;
作为 SELECT 语句,我可以使用内部联接来提取此信息.但是作为合并语句,我收到了上述错误.SYSTEM 是两个表中唯一匹配的数据.如何以不会出错的方式重写上述内容?
As a SELECT statement, I can pull this information using an inner join. But as a merge statement, I get the above error. SYSTEM is the only matching data in both tables. How can I rewrite the above in a way that it will not error out?
推荐答案
我将说明这个错误的根源是什么.
考虑以下简单示例:
I am going to show what the source of this error is.
Consider the below simple example:
CREATE TABLE A_100(
x_system int,
val int
);
INSERT INTO a_100 values( 1, 100 );
INSERT INTO a_100 values( 2, 200 );
CREATE TABLE B_100(
x_system int,
val int
);
INSERT INTO b_100 values( 1, 1100 );
INSERT INTO b_100 values( 2, 2000 );
INSERT INTO b_100 values( 2, 3000 );
commit;
<小时>
现在请考虑这个加入:
Now please consider this join:
SELECT *
FROM A_100 a
JOIN B_100 b
ON a.x_system = b.x_system AND a.x_system = 1
;
| X_SYSTEM | VAL | X_SYSTEM | VAL |
|----------|-----|----------|------|
| 1 | 100 | 1 | 1100 |
上述查询给出了表 B_100
中的一条唯一记录.如果在合并语句中使用此连接条件,合并将运行而不会出现任何错误:
the above query gives one unique record from the table B_100
. If you use this join condition in a merge statement, the merge will run without any error:
MERGE INTO A_100 a
USING B_100 b
ON ( a.x_system = b.x_system AND a.x_system = 1)
WHEN MATCHED THEN UPDATE SET a.val = b.val
;
1 row merged.
<小时>
现在请考虑以下连接:
Now please consider the below join:
SELECT *
FROM A_100 a
JOIN B_100 b
ON a.x_system = b.x_system AND a.x_system = 2
;
| X_SYSTEM | VAL | X_SYSTEM | VAL |
|----------|-----|----------|------|
| 2 | 200 | 2 | 2000 |
| 2 | 200 | 2 | 3000 |
上述连接,对于来自 A_100
的一条记录,给出来自 B_100
的两条记录.
如果您尝试使用具有上述连接条件的 MERGE,您将获得以下结果:
The above join, for one record from A_100
gives two records from B_100
.
If you try to use MERGE with the above join condition you will get the following:
MERGE INTO A_100 a
USING B_100 b
ON ( a.x_system = b.x_system AND a.x_system = 2)
WHEN MATCHED THEN UPDATE SET a.val = b.val
;
Error report -
ORA-30926: unable to get a stable set of rows in the source tables
<小时>
Oracle 简单地说:
Oracle simply says you :
从左表查询一条记录返回两个值:右表中的 2000 和 3000.
我无法将右表中的两个值分配给左表的单个标量字段,这是不可能的.
请更改连接条件,使其仅给出一个唯一的为左表中的每条记录从右表中记录
The query for one record from the left table returned two values: 2000 and 3000 from the right table.
I can not assign two values from the right table to a single scalar field of the left table, this is impossible.
Please change the join condition so that it gives only one unique record from the right table for each record in the left table
相关文章