简体   繁体   中英

Stored Procedure two exception select statements

I am having some trouble with my Stored Procedure in Oracle 12.1

The blow code is getting an error and I think it has to do with using two EXCEPTION statements in a row?

I am created a stored Procedure for a fake small online DVD company that updates the queue position in the rental queue table. I use an IF statement that says if the member already has this DVD in the queue, then updates the queue position. In order to check if the member already has a DVD in the queue I write an SQL Select statement that checks for the DVD in the members queue. If the statement finds a DVD then it inserts it into "dvd_check". If no DVD is found then i place DVD_check to equal 0.

I also have a 2nd SQL statement to determine the original queue order of the row that is about to be updated. Both Statements run great on their own within the stored procedure but when I put them together in the StoredProcedure before my IF statement, then I get an error:

Error(17,5): PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following: ( begin case declare end exit for goto if loop mod null... AND

Error(29,4): PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: end not pragma final instantiable order overriding static...

Here is my Code:

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;

Thanks so much for helping me solve this issue.

There is a missing END; in your code. Depending on how control is supposed to flow it might be supposed to look like this:

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;

You have exceptions in the middle of the code. You must structure your code to look like this:

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

   begin
     select ...
   exception
     when no_data_found ..
   end;
   ...
end;
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;

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