投掷太多行,但只选择了一个

时间:2018-08-21 20:47:50

标签: sql oracle stored-procedures plsql ora-01422

我有此过程,该过程仅基于称为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

这是怎么回事?

1 个答案:

答案 0 :(得分:5)

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