我是学习SQL的新手,目前正在课堂上学习SQL。我正在尝试编写满足以下要求的代码:
进行预订:输入参数:酒店,客人的姓名,开始日期,结束日期,房间类型,预订日期。输出:预订ID。注意:每次预订仅可容纳一位客人。但是,同一位客人可以进行多次预订。
查找预订:输入的是客人的姓名和日期,酒店ID。输出为预订ID
我仍然对解密错误代码有些陌生,并试图查找它们的含义。但是,我仍然不太确定为什么我的代码是错误的。
CREATE OR REPLACE PACKAGE hotelmanagement AS
FUNCTION make(rsrv_id VARCHAR2
,hotel_name VARCHAR2
,guest VARCHAR2
,start_date VARCHAR2
,end_date VARCHAR2
,room_type VARCHAR2
,rsrv_date VARCHAR2)
RETURN NUMBER IS
rsrv_id NUMBER;
BEGIN
SELECT rsrv_seq.nextval INTO reserve_id FROM dual;
INSERT INTO reservations
VALUES
(reserve_id, 'Four Seasons', 'Amanda', 'July-30-2019', 'Aug-8-2019',
'King', 'July-18-2019');
tot_rsrv := tot_rsrv + 1;
RETURN(rsrv_id);
END;
FUNCTION find(guest VARCHAR2
,rsrv_date VARCHAR2)
RETURN NUMBER IS
rsrv_id NUMBER;
BEGIN
SELECT rsrv_id
INTO guest
FROM reservations
WHERE rsrv_date = find_rsrv_date;
END;
RETURN(rsrv_id);
END hotelmanagement;
我有这些错误消息(其中两个看起来相同吗?):
错误(4,1):PLS-00103:预期出现以下情况时遇到符号“ SELECT”:begin函数pragma过程子类型类型当前游标已删除之前,符号“ begin”已替换为“ SELECT”继续。
错误(5,1):PLS-00103:预期以下情况之一时遇到符号“ RSRV_ID”:语言
错误(5,1):PLS-00103:在预期以下其中一种情况时遇到了符号“ RSRV_ID”
答案 0 :(得分:3)
要求非常模糊,因此很难提供所需的过程。无论如何,您的代码中有一些弱项。
VARCHAR2
。 VARCHAR2
仅用于字符串数据。至少您使用四位数的年份,这是正确的做法。RETURN
命令不使用方括号。考虑到所有这些,您的代码应该大致像这样:
CREATE OR REPLACE PACKAGE BODY hotelmanagement AS
FUNCTION make( -- you can't declare variable "rsrv_id" twice
hotel_name IN VARCHAR2
,guest IN VARCHAR2
,start_date IN DATE
,end_date IN DATE
,room_type IN VARCHAR2
,rsrv_date IN DATE)
RETURN NUMBER IS
rsrv_id NUMBER;
BEGIN
INSERT INTO reservations
VALUES
(rsrv_seq.nextval, hotel_name, guest, start_date, end_date,
room_type, rsrv_date)
RETURNING reserve_id INTO rsrv_id ;
-- tot_rsrv := tot_rsrv + 1; -> I don't see any use for it, variable tot_rsrv is not declared
RETURN rsrv_id;
END;
FUNCTION find(v_guest IN VARCHAR2
,rsrv_date IN DATE) RETURN NUMBER IS
rsrv_id NUMBER;
BEGIN
SELECT rsrv_id
INTO rsrv_id
FROM reservations
WHERE rsrv_date = find_rsrv_date
and guest = v_guest; -- don't use "guest = guest" because this will select all rows.
RETURN rsrv_id;
END;
END hotelmanagement;