ORACLE IF-ELSE 分支复杂化

ORACLE IF-ELSE Branching complication

我有一个关于 if else 分支在 oracle 过程中的问题。所以我有这个查询,

CREATE OR REPLACE PROCEDURE WELTESADMIN.SP_MST_ERC_UPD
(
    p_HEAD_MARK IN VARCHAR2,
    p_PROJECT_NAME IN VARCHAR2,
    p_COMP_TYPE IN VARCHAR2,
    p_ONSITE_UPD_QTY IN INTEGER,
    p_PREP_UPD_QTY IN INTEGER,
    p_ERECT_UPD_QTY IN INTEGER,
    p_QC_UPD_QTY IN INTEGER,
    p_UPD_SIGN IN VARCHAR
)
AS
/* IF NOT THEN.. ; WHEN THE RECORD EXISTS DO THIS... */
BEGIN 
UPDATE MST_ERC_UPD 
    SET 
        ONSITE_UPD_QTY = p_ONSITE_UPD_QTY,
        PREP_UPD_QTY = p_PREP_UPD_QTY,
        ERECT_UPD_QTY = p_ERECT_UPD_QTY,
        QC_UPD_QTY = p_QC_UPD_QTY
    WHERE
        HEAD_MARK = p_HEAD_MARK AND
        PROJECT_NAME = p_PROJECT_NAME AND
        COMP_TYPE = p_COMP_TYPE;

INSERT INTO DTL_ERC_UPD ( /* THIS ACTS AS A HISTORY TABLE THAT RECORDS EVERY OCCURENCE */
    HEAD_MARK, PROJECT_NAME, COMP_TYPE,
    ONSITE_UPD_QTY, PREP_UPD_QTY, ERECT_UPD_QTY, QC_UPD_QTY,
    UPD_DATE, UPD_SIGN, UPD_INFO
) VALUES (
    p_HEAD_MARK, p_PROJECT_NAME, p_COMP_TYPE,
    p_ONSITE_UPD_QTY, p_PREP_UPD_QTY, p_ERECT_UPD_QTY, p_QC_UPD_QTY,
    SYSDATE, p_UPD_SIGN, 'UPDATE'
);

/* WHEN THE RECORD IS NOT EXISTS DO THIS */      
            IF SQL%ROWCOUNT = 0 THEN
            INSERT INTO MST_ERC_UPD
                (
                    HEAD_MARK, PROJECT_NAME, COMP_TYPE,
                    ONSITE_UPD_QTY, PREP_UPD_QTY, ERECT_UPD_QTY, QC_UPD_QTY
                )
                VALUES
                (
                    p_HEAD_MARK, p_PROJECT_NAME, p_COMP_TYPE,
                    p_ONSITE_UPD_QTY, p_PREP_UPD_QTY, p_ERECT_UPD_QTY, p_QC_UPD_QTY
                );

            /* THIS ACTS AS A HISTORY TABLE THAT RECORDS EVERY OCCURENCE */
            INSERT INTO DTL_ERC_UPD
                (
                    HEAD_MARK, PROJECT_NAME, COMP_TYPE,
                    ONSITE_UPD_QTY, PREP_UPD_QTY, ERECT_UPD_QTY, QC_UPD_QTY,
                    UPD_DATE, UPD_SIGN, UPD_INFO
                )
                VALUES
                (         
                    p_HEAD_MARK, p_PROJECT_NAME, p_COMP_TYPE,
                    p_ONSITE_UPD_QTY, p_PREP_UPD_QTY, p_ERECT_UPD_QTY, p_QC_UPD_QTY,
                    SYSDATE, p_UPD_SIGN, 'NEWENTRY'
                );
            END IF;
COMMIT;
END SP_MST_ERC_UPD;

所以我在这里分支的问题是,不执行记录不存在时的查询(具有 'NEWENTRY' 状态的新插入永远不会执行)。请帮我解决这个分支问题...谢谢

您的问题是您正在测试 LAST sql 命令

之后是否没有受影响的行
--UPDATE
--INSERT 
IF SQL%ROWCOUNT = 0 THEN

这永远不会是真的,因为你的最后一个命令是 INSERT 命令。要测试您的更新命令是否更改了某些内容,您必须创建一个变量来存储更新命令修改后的行,如下所示:

...
AS
numChangedRows NUMBER;
BEGIN
   numChangedRows:=0; 
   --Your Update command
   numChangedRows:=SQL%ROWCOUNT;
   --Your insert command
   IF numChangedRows = 0 THEN
...