用于循环错误的游标

时间:2018-03-17 13:09:41

标签: sql oracle plsql

我正在尝试编写将插入表中的游标,但我收到错误,需要帮助。我收到的错误是ORA-06550。

    DECLARE
    CURSOR cur_rating IS
           SELECT bc.name, bc.title, bc.checkoutdate, bc.returneddate,    
               b.categoryname,b.publisher, ba.authorname
               FROM bookshelf_checkout bc INNER JOIN bookshelf b
               ON bc.title = b.title
               INNER JOIN bookshelf_author ba
               ON bc.title = ba.title
               FOR UPDATE NOWAIT;
            lv_totdays_num NUMBER(4) := 0;
            lv_rating_txt VARCHAR2(2);
    BEGIN
       FOR rec_rating IN cur_rating LOOP
           lv_totdays_num := rec_rating.returneddate -  
           rec_rating.checkoutdate;
       IF lv_totdays_num <= 10 THEN lv_rating_txt := 'DR';
       ELSIF lv_totdays_num <= 25 THEN lv_rating_txt := 'CR';
       ELSIF lv_totdays_num <= 35 THEN lv_rating_txt := 'BR';
       ELSE lv_rating_txt := 'A';
       END IF;
       INSERT INTO bookshelf_audit (title, publisher, categoryname,  
                                   new_rating, auditdate)
       VALUES      (rec_rating.title, rec_rating.publisher,   
                 rec_rating.categoryname, lv_rating_txt, sysdate)
       WHERE CURRENT OF cur_rating;
    END LOOP;
    COMMIT;
    END;

2 个答案:

答案 0 :(得分:2)

WHERE CURRENT OFUPDATE语句中的DELETE子句指出应该更新或删除从表中提取的最新行:

UPDATE table_name
  SET set_clause
  WHERE CURRENT OF cursor_name;

OR

DELETE FROM table_name
WHERE CURRENT OF cursor_name;

但不适用于INSERT声明。 因此,仅删除WHERE CURRENT OF cur_rating部分,您的代码将会运行。这会使您的INSERT声明为:

INSERT INTO bookshelf_audit (title, publisher, categoryname,  
                                   new_rating, auditdate)
       VALUES      (rec_rating.title, rec_rating.publisher,   
                 rec_rating.categoryname, lv_rating_txt, sysdate);

答案 1 :(得分:2)

您需要从where声明中删除insert ... values子句:

INSERT INTO bookshelf_audit
    (title, publisher, categoryname,  
     new_rating, auditdate)
VALUES
    (rec_rating.title, rec_rating.publisher,   
     rec_rating.categoryname, lv_rating_txt, sysdate)
WHERE CURRENT OF cur_rating;

应该是

INSERT INTO bookshelf_audit
    (title, publisher, categoryname,  
     new_rating, auditdate)
VALUES
    (rec_rating.title, rec_rating.publisher,   
     rec_rating.categoryname, lv_rating_txt, sysdate);