Too Many Rows throw 但只有一个被选中

Too Many Rows throwing but only one is selected

我有这个程序,它只删除基于名为 AppID 的列字段的行。此过程根据该行 AppID 列从另一个名为 AppNbr 的列获取值。该过程在尝试 SELECT 行时失败并出现 TOO_MANY_ROWS 异常。这是 PL/SQL:

DECLARE
    lvnApplNbr NUMBER;

    PROCEDURE deleteAppl(applId IN VARCHAR2) IS
    BEGIN
        BEGIN
            SELECT ApplNbr -- Exception thrown here
            INTO lvnApplNbr
            FROM Appl
            WHERE ApplID = applId;
        EXCEPTION
            WHEN TOO_MANY_ROWS THEN
                DBMS_OUTPUT.PUT_LINE(SQLERRM);
        END;
        -- ... Delete it after some logic
    END; -- End Procedure
BEGIN
    ...
    deleteAppl('571E00BA-70E6-4523-BEAC-4568C3DD1A7D');
    ...
END;

SELECT INTO 时抛出 TOO_MANY_ROWS 异常。我不知道为什么会抛出这个错误,因为如果我只是查询这个:

SELECT ApplNbr FROM Appl WHERE ApplId = '571E00BA-70E6-4523-BEAC-4568C3DD1A7D';

只有一行会返回正确的 ApplId

这是怎么回事?

只需为相关 table (Appl) 使用别名:

   PROCEDURE deleteAppl(applId IN VARCHAR2) IS
   .....
   .....
   SELECT ApplNbr 
    INTO lvnApplNbr
    FROM Appl a
    WHERE a.ApplID = applId;
   ......

或将您的参数名称 (applId) 更改为其他名称,例如 i_applId :

   PROCEDURE deleteAppl(i_applId IN VARCHAR2) IS
   .....
   .....
   SELECT ApplNbr 
    INTO lvnApplNbr
    FROM Appl
    WHERE ApplID = i_applId;
   ......

因为在您的情况下,如果您的 parameter's namecolumn name 相同,则感知到多重匹配。