0

我在 Oracle 12.1 中的存储过程遇到了一些问题

打击代码出现错误,我认为这与连续使用两个 EXCEPTION 语句有关?

我为一家伪造的小型在线 DVD 公司创建了一个存储过程,用于更新租赁队列表中的队列位置。我使用一个 IF 语句来说明该成员是否已经在队列中拥有这张 DVD,然后更新队列位置。为了检查成员队列中是否已经有 DVD,我编写了一个 SQL Select 语句来检查成员队列中的 DVD。如果语句找到 DVD,则将其插入“dvd_check”。如果没有找到 DVD,那么我将 DVD_check 设置为等于 0。

我还有第二条 SQL 语句来确定即将更新的行的原始队列顺序。这两个语句在存储过程中运行良好,但是当我在 IF 语句之前将它们放在 StoredProcedure 中时,我得到一个错误:

错误(17,5):PLS-00103:在预期以下情况之一时遇到符号“EXCEPTION”:(begin case declare end exit for goto if loop mod null... AND

错误(29,4):PLS-00103:在预期以下情况之一时遇到符号“文件结尾”:结束而不是编译指示最终可实例化的顺序覆盖静态...

这是我的代码:

CREATE OR REPLACE PROCEDURE sp_queueorder (
dvdid_sp number,
memberid_sp number,
queueposition_sp number)
IS
dvd_check number;
old_queueposition number;
BEGIN

SELECT dvdid INTO dvd_check FROM rentalqueue  
WHERE DVDid = dvdid_sp and memberid = memberid_sp;
EXCEPTION
WHEN no_data_found THEN dvd_check := 0;

SELECT queueposition INTO old_queueposition FROM rentalqueue  
WHERE DVDid = dvdid_sp and memberid = memberid_sp;
EXCEPTION
WHEN no_data_found THEN old_queueposition := 0;

IF dvd_check > 0 THEN

UPDATE rentalqueue SET queueposition = queueposition + 1
WHERE memberid = memberid_sp and queueposition >= queueposition_sp AND queueposition <=       old_queueposition;

UPDATE rentalqueue SET queueposition = queueposition_sp
WHERE dvdid = dvdid_sp AND memberid = memberid_sp;

END IF;
END;

非常感谢帮助我解决这个问题。

4

3 回答 3

0

缺少 END;在你的代码中。根据控制应该如何流动,它可能看起来像这样:

CREATE OR REPLACE PROCEDURE sp_queueorder (
dvdid_sp number,
memberid_sp number,
queueposition_sp number)
IS
dvd_check number;
old_queueposition number;
BEGIN

SELECT dvdid INTO dvd_check FROM rentalqueue  
WHERE DVDid = dvdid_sp and memberid = memberid_sp;
EXCEPTION
WHEN no_data_found THEN dvd_check := 0;
---add an END; to the block here?
END;
SELECT queueposition INTO old_queueposition FROM rentalqueue  
WHERE DVDid = dvdid_sp and memberid = memberid_sp;
EXCEPTION
WHEN no_data_found THEN old_queueposition := 0;
----or add an end here?
END;
IF dvd_check > 0 THEN

UPDATE rentalqueue SET queueposition = queueposition + 1
WHERE memberid = memberid_sp and queueposition >= queueposition_sp AND queueposition <=       old_queueposition;

UPDATE rentalqueue SET queueposition = queueposition_sp
WHERE dvdid = dvdid_sp AND memberid = memberid_sp;

END IF;
END;
--or add it here?
END;
于 2013-11-12T13:33:17.300 回答
0

您在代码中间有异常。您必须将代码构造成如下所示:

begin
   begin
     select ...
   exception
     when no_data_found ..
   end;

   begin
     select ...
   exception
     when no_data_found ..
   end;
   ...
end;
于 2013-11-12T13:56:57.390 回答
0
CREATE OR REPLACE PROCEDURE sp_queueorder (
    dvdid_sp number,
    memberid_sp number,
    queueposition_sp number)
IS
    dvd_check number;
    old_queueposition number;
BEGIN
    -- 1st select
    BEGIN
        SELECT dvdid INTO dvd_check FROM rentalqueue  
        WHERE DVDid = dvdid_sp and memberid = memberid_sp;
    EXCEPTION
        WHEN no_data_found THEN dvd_check := 0;
    END;

    -- 2nd select
    BEGIN
        SELECT queueposition INTO old_queueposition FROM rentalqueue  
        WHERE DVDid = dvdid_sp and memberid = memberid_sp;
    EXCEPTION
        WHEN no_data_found THEN old_queueposition := 0;
    END;

    IF dvd_check > 0 THEN
        UPDATE rentalqueue SET queueposition = queueposition + 1
        WHERE memberid = memberid_sp and queueposition >= queueposition_sp AND queueposition <= old_queueposition;

        UPDATE rentalqueue SET queueposition = queueposition_sp
        WHERE dvdid = dvdid_sp AND memberid = memberid_sp;

        COMMIT; -- if is not needed remove
    END IF;
END;
于 2013-11-12T14:03:44.513 回答