Oracle表单显示错误ORA-01422多条记录。

huangapple go评论96阅读模式
英文:

Oracle Form showing error ORA-01422 more than one record

问题

IF :CUSTOMER_ID IS NOT NULL AND :BRANCH_ID IS NOT NULL THEN
    DECLARE
        X_NO           NUMBER;
        X_VOUCHER_ID   NUMBER;
    BEGIN
        SELECT
            GL_VOUCHER_ID
        INTO X_VOUCHER_ID
        FROM
            SM_SALES_INVOICES SI,
            AR_CUSTOMERS AC,
            GL_VOUCHERS GV
        WHERE
            AC.CUSTOMER_ID = SI.CUSTOMER_ID
            AND SI.GL_VOUCHER_ID = GV.VOUCHER_ID
            AND SI.CUSTOMER_ID = :CUSTOMER_ID;

        UPDATE GL_VOUCHERS
        SET
            BRANCH_ID = :NEW_BRANCH_ID
        WHERE
            VOUCHER_ID = X_VOUCHER_ID;

        MESSAGE('Branch is changed successfully!');
        MESSAGE('Branch is changed successfully!');
    EXCEPTION
        WHEN NO_DATA_FOUND THEN
            NULL;
    END;

    COMMIT;
ELSE
    MESSAGE('Please Enter Both Fields!');
    MESSAGE('Please Enter both Fields!');
END IF;
英文:
IF :CUSTOMER_ID IS NOT NULL AND :BRANCH_ID IS NOT NULL THEN
    DECLARE
        X_NO           NUMBER;
        X_VOUCHER_ID   NUMBER;
    BEGIN
        SELECT
            GL_VOUCHER_ID
        INTO X_VOUCHER_ID
        FROM
            SM_SALES_INVOICES SI,
            AR_CUSTOMERS AC,
            GL_VOUCHERS GV
        WHERE
            AC.CUSTOMER_ID = SI.CUSTOMER_ID
            AND SI.GL_VOUCHER_ID = GV.VOUCHER_ID
            AND SI.CUSTOMER_ID = :CUSTOMER_ID;

        UPDATE GL_VOUCHERS
        SET
            BRANCH_ID = :NEW_BRANCH_ID
        WHERE
            VOUCHER_ID = X_VOUCHER_ID;

        MESSAGE('Branch is changed sucessfully!');
        MESSAGE('Branch is changed sucessfully!');
    EXCEPTION
        WHEN NO_DATA_FOUND THEN
            NULL;
    END;

    COMMIT;
ELSE
    MESSAGE('Please Enter Both Fields!');
    MESSAGE('Please Enter both Fields!');
END IF;

答案1

得分: 2

代码中的问题是,您的第一个 SELECT 查询返回了多条记录,而您试图将多个结果存储到单个变量 X_VOUCHER_ID 中。

您需要按照以下方式更新表 GL_VOUCHERS,而不将 GL_VOUCHER_ID 存储到 X_VOUCHER_ID 中:

IF :CUSTOMER_ID IS NOT NULL AND :BRANCH_ID IS NOT NULL THEN
    DECLARE
        X_NO           NUMBER;
    BEGIN
        UPDATE GL_VOUCHERS
        SET
            BRANCH_ID = :NEW_BRANCH_ID
        WHERE
            VOUCHER_ID IN (
                SELECT
                    GL_VOUCHER_ID
                FROM
                    SM_SALES_INVOICES SI,
                    AR_CUSTOMERS AC,
                    GL_VOUCHERS GV
                WHERE
                    AC.CUSTOMER_ID = SI.CUSTOMER_ID
                    AND SI.GL_VOUCHER_ID = GV.VOUCHER_ID
                    AND SI.CUSTOMER_ID = :CUSTOMER_ID
            );

        MESSAGE('Branch is changed successfully!');
        MESSAGE('Branch is changed successfully!');
    EXCEPTION
        WHEN NO_DATA_FOUND THEN
            NULL;
    END;

    COMMIT;
ELSE
    MESSAGE('Please Enter Both Fields!');
    MESSAGE('Please Enter both Fields!');
END IF;

以上代码将更新与内部子查询中的 GL_VOUCHER_ID 匹配的 GL_VOUCHERS 表的所有记录。

祝好!

英文:

The issue with your code is that your first SELECT query is returning multiple records and you are trying to store multiple results into single variable X_VOUCHER_ID.

You need to update the table GL_VOUCHERS without saving GL_VOUCHER_ID into X_VOUCHER_ID as following:

> IF :CUSTOMER_ID IS NOT NULL AND :BRANCH_ID IS NOT NULL THEN
> DECLARE
> X_NO NUMBER;
> -- removed following variable
> -- X_VOUCHER_ID NUMBER;
> BEGIN
> --REMOVED THIS SELECT CLAUSE
> -- SELECT
> -- GL_VOUCHER_ID
> -- INTO X_VOUCHER_ID
> -- FROM
> -- SM_SALES_INVOICES SI,
> -- AR_CUSTOMERS AC,
> -- GL_VOUCHERS GV
> -- WHERE
> -- AC.CUSTOMER_ID = SI.CUSTOMER_ID
> -- AND SI.GL_VOUCHER_ID = GV.VOUCHER_ID
> -- AND SI.CUSTOMER_ID = :CUSTOMER_ID;
> UPDATE GL_VOUCHERS
> SET
> BRANCH_ID = :NEW_BRANCH_ID
> WHERE
> VOUCHER_ID IN ( -- ADDED THIS IN CLAUSE
> SELECT
> GL_VOUCHER_ID
> FROM
> SM_SALES_INVOICES SI,
> AR_CUSTOMERS AC,
> GL_VOUCHERS GV
> WHERE
> AC.CUSTOMER_ID = SI.CUSTOMER_ID
> AND SI.GL_VOUCHER_ID = GV.VOUCHER_ID
> AND SI.CUSTOMER_ID = :CUSTOMER_ID
> );
>
> MESSAGE('Branch is changed sucessfully!');
> MESSAGE('Branch is changed sucessfully!');
> EXCEPTION
> WHEN NO_DATA_FOUND THEN
> NULL;
> END;
>
> COMMIT;
> ELSE
> MESSAGE('Please Enter Both Fields!');
> MESSAGE('Please Enter both Fields!');
> END IF;

The above code will update all the records of the GL_VOUCHERS table which matches the GL_VOUCHER_ID from an inner subquery.

Cheers!!

huangapple
  • 本文由 发表于 2020年1月6日 19:26:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定