背景:我正在使用oracle SQLDeveloper,我有表:
TUTPRAC :CLASSID(PK),UNITCODE,STAFFNO,CLASSDAY,CLASSTIME,CLASSTYPE,ROOMNUM
UNITSTREAM :STREAMID(PK),UNITCODE,STAFFNO,DAY,TIME,LOCATION
问题:即使记录在日期,时间和房间方面完全独特,无论我尝试使用该程序输入的内容,我的当前代码都不会输入记录。
我尝试使用该程序检查是否有任何课程已占用我尝试使用该程序输入的记录的日期/时间/房间。
要成功添加记录=单位(例如COMP111(UNITCODE))在同一天不能有任何课程(无论时间或地点)。它也不能被指定为与另一个单元相同的房间,以防止两个课程被预订到同一个教室(例如SCIE112)。最后,单元不能与单元讲座相同(讲座详细信息存储在UNITSTREAM表中)。
TUTPRACS包含教程和实践的记录,而UNITSTREAM包含流的记录(讲座)。
我是PL / SQL的新手,所以任何帮助都会非常感激。到目前为止我的代码:
CREATE OR REPLACE PROCEDURE OPEN_CLASS(
p_class IN TUTPRAC.CLASSID%TYPE,
p_unitc IN TUTPRAC.UNITCODE%TYPE,
p_classd IN TUTPRAC.CLASS_DAY%TYPE,
p_classt IN TUTPRAC.CLASS_TIME%TYPE,
p_classtp IN TUTPRAC.CLASS_TYPE%TYPE,
p_roomnm IN TUTPRAC.ROOMNUM%TYPE)
IS
x NUMBER:=0;
y NUMBER:=0;
BEGIN
-- checks
SELECT nvl((SELECT 1 FROM TUTPRAC WHERE CLASS_DAY = p_classd and CLASS_TIME = p_classt and CLASS_TYPE = p_classtp and ROOMNUM = p_roomnm) , 0) INTO x FROM dual;
SELECT nvl((SELECT 1 FROM UNITSTREAM WHERE UNITCODE = p_unitc and DAY = p_classd and TIME = p_classt and LOCATION = p_roomnm) , 0) INTO y FROM dual;
-- insert
IF (x = 1 and y = 1) THEN
INSERT INTO TUTPRAC ("CLASSID", "UNITCODE", "CLASS_DAY", "CLASS_TIME", "CLASS_TYPE", "ROOMNUM")
VALUES (p_class, p_unitc, p_classd, p_classt, p_classtp, p_roomnm);
END IF;
END OPEN_CLASS;
答案 0 :(得分:1)
首先尝试这个。您可能希望以不同方式处理异常和/或向调用此过程的程序返回一些内容,以使其知道“成功”或“失败”。但是如果你愿意,我们可以稍后再努力。
CREATE OR REPLACE PROCEDURE OPEN_CLASS(
p_class IN TUTPRAC.CLASSID%TYPE,
p_unitc IN TUTPRAC.UNITCODE%TYPE,
p_classd IN TUTPRAC.CLASS_DAY%TYPE,
p_classt IN TUTPRAC.CLASS_TIME%TYPE,
p_classtp IN TUTPRAC.CLASS_TYPE%TYPE,
p_roomnm IN TUTPRAC.ROOMNUM%TYPE)
IS
x NUMBER := 0;
y NUMBER := 0;
exc_clash EXCEPTION;
PRAGMA EXCEPTION_INIT(exc_clash, -22000);
BEGIN
-- checks for conflicts
SELECT 1
INTO x
FROM TUTPRAC
WHERE CLASS_DAY = p_classd
and CLASS_TIME = p_classt
and CLASS_TYPE = p_classtp
and ROOMNUM = p_roomnm;
SELECT 1
INTO y
FROM UNITSTREAM
WHERE UNITCODE = p_unitc
and DAY = p_classd
and TIME = p_classt
and LOCATION = p_roomnm;
-- exception if conflict else insert class.
IF (x = 1 OR y = 1) THEN
RAISE exc_clash;
ELSE
INSERT INTO TUTPRAC ("CLASSID", "UNITCODE", "CLASS_DAY", "CLASS_TIME", "CLASS_TYPE", "ROOMNUM")
VALUES (p_class, p_unitc, p_classd, p_classt, p_classtp, p_roomnm);
COMMIT;
END IF;
EXCEPTION
WHEN exc_clash THEN
DBMS_OUTPUT.PUT_LINE('There is a class clash');
END OPEN_CLASS;
答案 1 :(得分:0)
我在检查中使用和+或首先检查班级+日冲突,然后检查时间/日/房间冲突。似乎现在工作。
BEGIN
-- checks
SELECT nvl((SELECT 1 FROM TUTPRAC WHERE UNITCODE = p_unitc and CLASS_DAY = p_classd or CLASS_DAY = p_classd and CLASS_TIME = p_classt and ROOMNUM = p_roomnm) , 0) INTO x FROM dual;
SELECT nvl((SELECT 1 FROM UNITSTREAM WHERE UNITCODE = p_unitc and DAY = p_classd or DAY = p_classd and TIME = p_classt and LOCATION = p_roomnm) , 0) INTO y FROM dual;
-- insert
IF (x = 0 and y = 0) THEN
INSERT INTO TUTPRAC (CLASSID, UNITCODE, CLASS_DAY, CLASS_TIME, CLASS_TYPE, ROOMNUM)
VALUES (p_class, p_unitc, p_classd, p_classt, p_classtp, p_roomnm);
END IF;
END OPEN_CLASS;