ORACLE IF-ELSE分支并发症

时间:2015-01-24 03:25:56

标签: oracle plsql

我有一个关于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'状态的新插入永远不会被执行)。请帮我解决这个分支问题...谢谢

1 个答案:

答案 0 :(得分:1)

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

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

由于您的上一个命令是INSERT命令,因此永远不会成立。要测试您的Update命令是否已更改某些内容,您必须创建一个变量以通过update命令存储修改后的行,如下所示:

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