运行创建的过程时出现Oracle错误

时间:2013-02-26 16:20:01

标签: oracle plsql procedure

我有基于酒店房间的Oracle数据库。 我创建了一个Room_taken程序,但我无法使其正常工作。

这是我的SQL过程代码:

CREATE
PROCEDURE Room_taken
(
v_guestID IN NUMBER DEFAULT NULL ,
v_stayfrom IN DATE DEFAULT NULL ,
v_stayto IN DATE DEFAULT NULL ,
v_roomID IN NUMBER DEFAULT NULL ,
v_paymentID IN NUMBER DEFAULT NULL ,
v_totalprice IN NUMBER DEFAULT NULL 
)
AS
v_nofrom DATE;
v_noto DATE;
v_idroom NUMBER(10,0);

BEGIN

BEGIN
SELECT Stay_from, Stay_to     
INTO v_nofrom, v_noto
FROM Stayover
WHERE ID_room = v_roomID;
IF NOT ( v_nofrom >= v_stayfrom
AND v_noto <= v_stayto ) THEN

BEGIN
INSERT INTO Stayover
( Stay_from, Stay_to, Total_price, ID_Room, ID_Guest, ID_Payment )
VALUES ( v_stayfrom, v_stayto, v_totalprice, v_roomID, v_guestID, v_paymentID );
END;
ELSE

BEGIN
raise_application_error( -20002, 'Room is taken!' );
END;
END IF;
END;
END;

输入以下参数后运行程序时:

DECLARE
v_guestID NUMBER;
v_stayfrom DATE;
v_stayto DATE;
v_roomID NUMBER;
v_paymentID NUMBER;
v_totalprice NUMBER;
BEGIN
v_guestID := 1;
v_stayfrom := '17.01.2012';
v_stayto := '19.01.2012';
v_roomID := 1;
v_paymentID := 1;
v_totalprice := 300;

Room_taken(
v_guestID => v_guestID,
v_stayfrom => v_stayfrom,
v_stayto => v_stayto,
v_roomid => v_roomid,
v_paymentID => v_paymentID,
v_totalprice => v_totalprice
);
END;

我收到了错误消息:

Connecting to the database HOTEL.
ORA-01403: no data found
ORA-06512: at "HOTEL_ROOMS.ROOM_TAKEN", line 18
ORA-06512: at line 16
Process exited.
Disconnecting from the database HOTEL.

我做错了什么?

2 个答案:

答案 0 :(得分:4)

您收到错误,因为SELECT INTO构造必须在PL / SQL中返回1行。您的第一个选择不返回任何行,因此会引发NO_DATA_FOUND错误。

如果有2行(例如1月份的预订,2月份的另一次预订),则会引发TOO_MANY_ROWS错误。

您要做的是确保插入时,不重叠。例如,以下查询将在一段时间内返回所有预订:

SELECT * 
  FROM Stayover 
 WHERE ID_room = v_roomID
   AND stay_from <= v_stayto 
   AND stay_to >= v_stayfrom 

请注意,我将stay_fromv_stayto进行了比较,这不是一个错字。

您可以使用以下内容将上述查询包含在您的代码中:

CREATE OR REPLACE PROCEDURE Room_taken(v_guestID    IN NUMBER,
                                       v_stayfrom   IN DATE,
                                       v_stayto     IN DATE,
                                       v_roomID     IN NUMBER,
                                       v_paymentID  IN NUMBER DEFAULT NULL,
                                       v_totalprice IN NUMBER DEFAULT NULL) AS
   l_nb_reservation NUMBER;
BEGIN
   SELECT COUNT(*)
     INTO l_nb_reservation
     FROM Stayover
    WHERE ID_room = v_roomID
      AND stay_from <= v_stayto
      AND stay_to >= v_stayfrom;
   IF l_nb_reservation > 0 THEN
      raise_application_error(-20002, 'Room is taken!');
   END IF;

   INSERT INTO Stayover
      (Stay_from, Stay_to, Total_price, ID_Room, ID_Guest, ID_Payment)
   VALUES
      (v_stayfrom, v_stayto, v_totalprice, v_roomID, v_guestID, v_paymentID);
END;

现在,您每个时期只能预订一次房间:

SQL> EXEC Room_taken(1, trunc(SYSDATE), trunc(SYSDATE), 1);

PL/SQL procedure successfully completed

SQL> EXEC Room_taken(1, trunc(SYSDATE), trunc(SYSDATE), 1);

begin Room_taken(1, trunc(SYSDATE), trunc(SYSDATE), 1); end;

ORA-20002: Room is taken!
ORA-06512: à "APPS.ROOM_TAKEN", ligne 16
ORA-06512: à ligne 2

答案 1 :(得分:1)

错误讯息
ORA-01403 :未找到数据

错误原因

您尝试了以下操作之一:

  1. 您执行了SELECT INTO语句,但未返回任何行。
  2. 您在表格中引用了未初始化的行。
  3. 您使用UTL_FILE包读取文件末尾。
  4. 显示建议的解决方案
    检查此链接
    http://www.techonthenet.com/oracle/errors/ora01403.php