存储过程有两个异常选择语句

时间:2013-11-12 13:29:41

标签: sql oracle stored-procedures

我在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”:(如果循环mod为null,则开始为goto声明结束退出... 和

错误(29,4):PLS-00103:遇到以下其中一项时遇到符号“end-of-file”:end not pragma final instantiable order overriding static ...

这是我的代码:

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;

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

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

代码中间有例外。您必须将代码结构化为:

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

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

答案 2 :(得分: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;