尝试使用Merge Into而不是Update时出错

时间:2018-03-21 22:45:49

标签: sql database oracle plsql

我正在尝试加快我的更新并将我的update / set语句转换为合并到/使用。

旧版

ALTER SESSION ENABLE PARALLEL DML;
UPDATE /*+ PARALLEL(16) */ TEST_REPORT_2 rep 
SET    ( title ) = (
SELECT /*+ PARALLEL(16) */ doctitle.valstr Title 
FROM   MV_LLATTRDATA_SHRUNK_V3 doctitle 
WHERE  doctitle.id = rep.dataid 
    AND doctitle.defid = 3072256 
    AND doctitle.attrid = 5 
    AND doctitle.vernum = (SELECT MV.MAX_VERNUM
                                FROM   MV_LLATTRDATA_MAX_VERSIONS_V1 MV
                                WHERE  MV.id = rep.dataid
                                    AND defid = 3072256 
                                    AND attrid = 5) 
    AND doctitle.defvern = (SELECT MV.MAX_DEFVERN
                                FROM   MV_LLATTRDATA_MAX_VERSIONS_V1 MV
                                WHERE  MV.id = rep.dataid  
                                    AND defid = 3072256 
                                    AND attrid = 5)); 

新版

MERGE INTO TEST_REPORT_2 REP
USING MV_LLATTRDATA_SHRUNK_V3 doctitle
    ON (REP.DATAID = doctitle.ID
        AND doctitle.defid = 3072256 
        AND doctitle.attrid = 5 
        AND doctitle.vernum = (SELECT MV.MAX_VERNUM
                                FROM   MV_LLATTRDATA_MAX_VERSIONS_V1 MV
                                WHERE  MV.id = rep.dataid
                                    AND defid = 3072256 
                                    AND attrid = 5) 
        AND doctitle.defvern = (SELECT MV.MAX_DEFVERN
                                FROM   MV_LLATTRDATA_MAX_VERSIONS_V1 MV
                                WHERE  MV.id = rep.dataid))
WHEN MATCHED THEN UPDATE SET
    TITLE = doctitle.VALSTR;

但是我收到一条错误说:“ORA-01427:单行子查询返回多行”

2 个答案:

答案 0 :(得分:1)

defvern的逻辑可能需要包括defid和attrid`?

    AND doctitle.vernum = (SELECT MV.MAX_VERNUM
                            FROM   MV_LLATTRDATA_MAX_VERSIONS_V1 MV
                            WHERE  MV.id = rep.dataid
                                AND defid = 3072256 
                                AND attrid = 5) 
    AND doctitle.defvern = (SELECT MV.MAX_DEFVERN
                            FROM   MV_LLATTRDATA_MAX_VERSIONS_V1 MV
                            WHERE  MV.id = rep.dataid
                                AND defid = 3072256 
                                AND attrid = 5) 

这就是update中逻辑的结构。

答案 1 :(得分:0)

此表单的一个选择语句:

some_column = (select x from y where z)

重新调整多个值。

一个简单的解决方法是:

some_column = (select max(x) from y where z)

但不管怎样,你必须强制这个选择只返回一个值