插入不返回正确的值

时间:2017-06-27 12:14:22

标签: sql oracle insert sql-insert

所以我有一个更新,我正在尝试更改为INSERT

以下是原始更新声明(需要很长时间才能发布)

DECLARE
    BEGIN
        UPDATE TOCTASK_ORIG TASKO
            SET c_ocactvty_childactiviti = 11200,
                i_ocactvty_childactiviti = 
                (SELECT PROI.I
                FROM TOCPROCESSINSTANCE PROI,
                    TOCSERVICEREQUEST SR
                WHERE TASKO.c_occontbs_tasks = 11011
                    AND TASKO.i_occontbs_tasks = SR.I
                    AND PROI.C_PROCESSIServiceRe = SR.C
                    AND PROI.I_PROCESSIServiceRe = SR.I
                )
            WHERE EXISTS
                      (SELECT 1
                        FROM TOCPROCESSINSTANCE PROI,
                            TOCSERVICEREQUEST SR
                        WHERE TASK.c_occontbs_tasks = 11011
                            AND TASK.i_occontbs_tasks = SR.I
                            AND PROI.C_PROCESSIServiceRe = SR.C
                            AND PROI.I_PROCESSIServiceRe = SR.I
                        );
        COMMIT; 
    END;

通过在线研究,最好的方法是将其重写为插入并沿途进行更改

我在这里完成的是INSERT的SELECT子句

SELECT
TASKO.C
,TASKO.I
,11200  
,PROI.I
FROM TOCTASK_ORIG TASKO,
    TOCPROCESSINSTANCE PROI,
    TOCSERVICEREQUEST SR
WHERE TASKO.c_occontbs_tasks = 11011
    AND TASKO.i_occontbs_tasks = SR.I
    AND PROI.C_PROCESSIServiceRe = 11011
    AND PROI.I_PROCESSIServiceRe = SR.I;

问题是我目前在原始表中有110302条记录,但是当我将插入运行到新的临时表时,它只创建了19,269条记录。此数字与WHERE子句的第二部分匹配,其中19,269现在应与PROI.I值匹配,其余部分应为null

AND PROI.C_PROCESSIServiceRe = 11011
AND PROI.I_PROCESSIServiceRe = SR.I

问题是没有创建其他记录,只是创建了19,269条记录并停止了

任何帮助都很棒

2 个答案:

答案 0 :(得分:0)

如果您观察到,您当前正在执行INNER JOIN,因此只会出现匹配的行。而你实际上需要执行LEFT OUTER JOIN之类的

FROM TOCTASK_ORIG TASKO
LEFT JOIN     TOCSERVICEREQUEST SR ON TASKO.i_occontbs_tasks = SR.I
LEFT JOIN TOCPROCESSINSTANCE PROI ON PROI.I_PROCESSIServiceRe = SR.I
AND PROI.C_PROCESSIServiceRe = 11011
WHERE TASKO.c_occontbs_tasks = 11011;

答案 1 :(得分:0)

能够根据上面提供的一些信息找出答案

FROM TOCTASK_ORIG TASKO
LEFT OUTER JOIN TOCSERVICEREQUEST SR
ON TASKO.c_occontbs_tasks = SR.C
    AND TASKO.i_occontbs_tasks = SR.I
LEFT OUTER JOIN TOCPROCESSINSTANCE PROI
ON PROI.C_PROCESSIServiceRe = SR.C
    AND PROI.I_PROCESSIServiceRe = SR.I;