所以,我创建了以下过程:
--First Stored Procedure--
CREATE OR REPLACE PROCEDURE sp_GetMileageBalance (StartMileage IN NUMBER,EndMileage IN NUMBER, ExpDate IN DATE)
IS --Code declaration section--
--variables to store column values returned from select into
fPassengerID VARCHAR2(10);
pFirst VARCHAR2(20);
pLast VARCHAR2(20);
fFreqflyernum NUMBER (10);
fFreqflyerMileage NUMBER (7);
fMileagebalance NUMBER (7);
fMileageExpDate DATE;
MileageExpDate DATE;
MileageStart NUMBER (7);
MileageEnd NUMBER (7);
MileageBalance NUMBER (7);
--Declare Cursor
CURSOR cur_FreqFlyer IS
--Query cursor will point to results
SELECT F.FPASSENGERID, P.First, P.Last, F.FREQFLYERNUM, F.FREQFLYERMILEAGE, F.MILEAGEBALANCE, F.MILEAGEEXPDATE
INTO fPassengerID, pFirst,pLast,fFreqflyernum,fFreqflyerMileage,fMileagebalance,fMileageExpDate
FROM FREQUENT_FLYER F
INNER JOIN PASSENGER P
ON F.FPassengerID = P.PassengerID
WHERE F.MileageBalance >= StartMileage AND F.MILEAGEBALANCE <= EndMileage
AND MileageExpDate > ExpDate;
--Start Execution section--
BEGIN
--Open Cursor
OPEN cur_FreqFlyer; -- open cursor for use
--loop to display each record returned by cursor
--Use PL/SQL language control or loop to display each record pointed by cursor
LOOP
--Fetch cursor data
FETCH cur_FreqFlyer INTO fPassengerID,pFirst,pLast,fFreqflyernum,fFreqflyerMileage,
fMileageBalance,fMileageExpDate;
EXIT WHEN cur_FreqFlyer%NOTFOUND;
--Display each record
--Displaying the results
DBMS_OUTPUT.PUT_LINE ('CUSTOMER INFORMATION: ');
DBMS_OUTPUT.PUT_LINE (' ');
DBMS_OUTPUT.PUT_LINE ('The Frequent Flyer PassengerID is: ' ||fPassengerID);
DBMS_OUTPUT.PUT_LINE ('First Name of passenger is: ' ||pFirst);
DBMS_OUTPUT.PUT_LINE ('Last Name of passenger is: ' ||pLast);
DBMS_OUTPUT.PUT_LINE ('Frequent Flyer number of passenger is: ' ||fFreqflyernum);
DBMS_OUTPUT.PUT_LINE ('Frequent Flyer Mileage of Passenger is: ' ||fFreqflyerMileage);
DBMS_OUTPUT.PUT_LINE ('Frequent Flyer Balance of passenger is: ' ||fMileageBalance );
DBMS_OUTPUT.PUT_LINE ('Mileage expiration date of passenger is: ' ||fMileageExpDate);
DBMS_OUTPUT.PUT_LINE (' ');
END LOOP;
CLOSE cur_FreqFlyer; --close cursor
END sp_GetMileageBalance;
当我运行它时,它按预期工作 . 当我运行存储过程时:
SET SERVEROUTPUT ON
EXECUTE sp_GetMileageBalance (10000,20000,'01-OCT-17');
我得到了我的结果:
CUSTOMER INFORMATION:
The Frequent Flyer PassengerID is: KL87DF34DS
First Name of passenger is: Michelle
Last Name of passenger is: Mullington
Frequent Flyer number of passenger is: 9374392018
Frequent Flyer Mileage of Passenger is: 400
Frequent Flyer Balance of passenger is: 15000
Mileage expiration date of passenger is: 19-DEC-20
>PL/SQL procedure successfully completed.
但是,当我试图提出异常时:
EXCEPTION
WHEN no_data_found THEN
-- do something
dbms_output.put_line('Results not found.Try again ');
它给了我这个错误:
LINE / COL ERROR
54/2 PLS-00103:当遇到以下情况之一时遇到符号“EXCEPTION”:(如果循环mod为空pragma引发返回选择更新,则使用<< begin close current delete delete insert insert open rollback) savepoint set sql execute commit forall merge pipe purge
62/5 PLS-00103:遇到以下其中一项时遇到符号“CLOSE”:end not pragma final instantiable order overriding static member constructor map
不知道为什么要把这个吐回来 .
这是我把它放在我的代码中的方式:
--First Stored Procedure--
CREATE OR REPLACE PROCEDURE sp_GetMileageBalance (StartMileage IN NUMBER,EndMileage IN NUMBER, ExpDate IN DATE)
IS --Code declaration section--
--variables to store column values returned from select into
fPassengerID VARCHAR2(10);
pFirst VARCHAR2(20);
pLast VARCHAR2(20);
fFreqflyernum NUMBER (10);
fFreqflyerMileage NUMBER (7);
fMileagebalance NUMBER (7);
fMileageExpDate DATE;
MileageExpDate DATE;
MileageStart NUMBER (7);
MileageEnd NUMBER (7);
MileageBalance NUMBER (7);
--Declare Cursor
CURSOR cur_FreqFlyer IS
--Query cursor will point to results
SELECT F.FPASSENGERID, P.First, P.Last, F.FREQFLYERNUM, F.FREQFLYERMILEAGE, F.MILEAGEBALANCE, F.MILEAGEEXPDATE
INTO fPassengerID, pFirst,pLast,fFreqflyernum,fFreqflyerMileage,fMileagebalance,fMileageExpDate
FROM FREQUENT_FLYER F
INNER JOIN PASSENGER P
ON F.FPassengerID = P.PassengerID
WHERE F.MileageBalance >= StartMileage AND F.MILEAGEBALANCE <= EndMileage
AND MileageExpDate > ExpDate;
--Start Execution section--
BEGIN
--Open Cursor
OPEN cur_FreqFlyer; -- open cursor for use
--loop to display each record returned by cursor
--Use PL/SQL language control or loop to display each record pointed by cursor
LOOP
--Fetch cursor data
FETCH cur_FreqFlyer INTO fPassengerID,pFirst,pLast,fFreqflyernum,fFreqflyerMileage,
fMileageBalance,fMileageExpDate;
EXIT WHEN cur_FreqFlyer%NOTFOUND;
--Display each record
--Displaying the results
DBMS_OUTPUT.PUT_LINE ('CUSTOMER INFORMATION: ');
DBMS_OUTPUT.PUT_LINE (' ');
DBMS_OUTPUT.PUT_LINE ('The Frequent Flyer PassengerID is: ' ||fPassengerID);
DBMS_OUTPUT.PUT_LINE ('First Name of passenger is: ' ||pFirst);
DBMS_OUTPUT.PUT_LINE ('Last Name of passenger is: ' ||pLast);
DBMS_OUTPUT.PUT_LINE ('Frequent Flyer number of passenger is: ' ||fFreqflyernum);
DBMS_OUTPUT.PUT_LINE ('Frequent Flyer Mileage of Passenger is: ' ||fFreqflyerMileage);
DBMS_OUTPUT.PUT_LINE ('Frequent Flyer Balance of passenger is: ' ||fMileageBalance );
DBMS_OUTPUT.PUT_LINE ('Mileage expiration date of passenger is: ' ||fMileageExpDate);
DBMS_OUTPUT.PUT_LINE (' ');
EXCEPTION
WHEN no_data_found THEN
-- do something
dbms_output.put_line('Results not found.Try again ');
END LOOP;
CLOSE cur_FreqFlyer; --close cursor
END sp_GetMileageBalance;
2 回答
EXCEPTION是PL / SQL块结构的一部分 .
每个部分都有特定的职责,并且仅限于与责任相关的代码 . 所以DECLARE部分用于声明(变量,本地过程),BEGIN部分用于过程代码,EXCEPTION部分用于处理异常 .
通过将
exception
关键字放在您所做的位置,您终止了BEGIN部分并启动了异常处理程序 .close
不是处理异常的有效语句,因此编译器会抛出异常 .你可以像这样实现它: