错误 - ORA-01422: 精确提取 returns 多于请求的行数

Error - ORA-01422: exact fetch returns more than requested number of rows

我正在尝试执行以下程序,但一直收到以下信息:

代码:

SET SERVEROUTPUT ON
DECLARE
  aID VARCHAR2(4):= '&AuthID';
  aName BK_AUTHOR.FNAME%TYPE;
  aSurname BK_AUTHOR.LNAME%TYPE;
  bTit BK_BOOKS.TITLE%TYPE;
  bCat BK_BOOKS.CATEGORY%TYPE;
  bPrice BK_COSTS.COST%TYPE;

BEGIN
 book_info(aID,aName,aSurname,bTit,bCat,bPrice);
  DBMS_OUTPUT.PUT_LINE(aName||'-'||aSurname||'wrote the book ('||bTit||') that is in category '||bCat||' and it cost R'||TO_CHAR(bPrice,'99999.99'));
END;
/

错误

ERROR at line 1:
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at "SYS.BOOK_INFO", line 13
ORA-06512: at line 9

从评论中添加代码:

CREATE OR REPLACE PROCEDURE book_info(
    AuthID IN OUT VARCHAR2,
    Name OUT BK_AUTHOR.FNAME%TYPE,
    Surname OUT BK_AUTHOR.LNAME%TYPE,
    Tit OUT BK_BOOKS.TITLE%TYPE,
    Cat OUT BK_BOOKS.CATEGORY%TYPE,
    Price OUT BK_COSTS.COST%TYPE )
IS
BEGIN
  SELECT FNAME,
    LNAME,
    TITLE,
    CATEGORY,
    COST
  INTO Name,
    Surname,
    Tit,
    Cat,
    Price
  FROM BK_BOOKS,
    BK_AUTHOR,
    BK_COSTS
  WHERE BK_COSTS.ISBN   = BK_BOOKS.ISBN
  AND BK_BOOKS.AUTHORID = AuthID;
END book_info;
/ 

可能是由于多种原因查看 book_info 的代码。

首先 - bk_books 和 bk_author 之间存在交叉连接。您需要在这两个表之间添加连接条件,例如 -

WHERE BK_COSTS.ISBN = BK_BOOKS.ISBN AND bk_books.authorid = bk_author.authorid AND AND BK_BOOKS.AUTHORID = AuthID

您还应该根据 Authorid 检查这些表的结果连接中的重复项。

编辑: 忘了说,请不要使用 SYS 用户进行任何与开发相关的活动,如创建过程或函数。从错误消息来看,似乎 book_info 过程在 SYS 用户中。除非您是 DBA 并将其用于与 sysdba 相关的活动,否则永远不要登录该用户。