简体   繁体   中英

PL/SQL Error Return Multiple Rows

I want to store some results in a temp table after I update a table but it is giving me an error for categories/book types that have multiple entries. Everything works fine when I try to do it with a book type that has one entry. Can someone tell me how I would fix this, I think it's got something to do with not having a filter although I'm not sure how I would do that.

DROP PROCEDURE UpdateAdvance;
CREATE OR REPLACE PROCEDURE UpdateAdvance (
  p_BookType          titles.category%TYPE,
  p_NewAdvance        advance_temptable.new_advance%TYPE) AS
  CURSOR titles_cur IS
  SELECT *
  FROM titles
  WHERE category=p_BookType
  FOR UPDATE OF advance;
  titles_rec titles_cur%ROWTYPE;
  v_OldAdvance        titles.advance%TYPE;
  v_TitleID           titles.title_id%TYPE;
  v_Title             titles.title%TYPE;
BEGIN
  SELECT advance, title_id, title
    INTO v_OldAdvance, v_TitleID, v_Title
    FROM titles
    WHERE category=p_BookType;

  FOR titles_rec in titles_cur LOOP  
      INSERT INTO advance_temptable VALUES(advance_sequence.nextval, v_TitleID, v_Title, v_OldAdvance, p_NewAdvance);
            END LOOP; 
    COMMIT;
END UpdateAdvance;

When I call it like this

BEGIN
  UpdateAdvance('psychology', 100);
END;

I get the error:

Error starting at line : 35 in command -
BEGIN
  UpdateAdvance('psychology', 100);
END;
Error report -
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at "SYSTEM.UPDATEADVANCE", line 14
ORA-06512: at line 2
01422. 00000 -  "exact fetch returns more than requested number of rows"
*Cause:    The number specified in exact fetch is less than the rows returned.
*Action:   Rewrite the query or change number of rows requested

Applied solution and here is what I now have working.

DROP PROCEDURE UpdateAdvance;
CREATE OR REPLACE PROCEDURE UpdateAdvance (
  p_BookType          titles.category%TYPE,
  p_NewAdvance        advance_temptable.new_advance%TYPE) AS
BEGIN
  INSERT INTO advance_temptable 
  select advance_sequence.nextval, title_id, title, advance, p_NewAdvance
  FROM titles
  WHERE category=p_BookType;
    COMMIT;
END UpdateAdvance;

BEGIN
  UpdateAdvance('psychology', 100);
END;

Why don't you do:

INSERT INTO advance_temptable 
  select advance_sequence.nextval, TitleID, Title, Advance, p_NewAdvance
  FROM titles
  WHERE category = p_BookType;

without cursor?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM