我有此过程,该过程仅基于称为AppID的列字段删除行。此过程基于该行AppNbr列从另一个称为AppID的列中获取值。当该过程尝试对行进行TOO_MANY_ROWS时,该过程以SELECT异常失败。这是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;

TOO_MANY_ROWS时,将引发SELECT INTO异常。我不知道为什么会引发该错误,因为如果我只是查询以下内容:
SELECT ApplNbr FROM Appl WHERE ApplId = '571E00BA-70E6-4523-BEAC-4568C3DD1A7D';

只有一行会返回正确的ApplId

到底是怎么回事?

最佳答案

只需为相关表使用别名(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是相同的,则会发现多个匹配项。

09-26 12:35