背景: 我有表格:
TUTPRAC: CLASSID,UNITCODE,STAFFNO,CLASSDAY,CLASSTIME,CLASSTYPE,ROOMNUM
UNITSTREAM: STREAMID,UNITCODE,STAFFNO,DAY,TIME,LOCATION
问题: 所以我有一个数据库和表格,我试图创建一个函数,在打开一个新类之前检查一个类是否打开,该 程序必须确保之间没有任何重叠(在时间或地点方面发生冲突) 该特定单元的流(讲座),tute会话和实践课程。 TUTPRACS包含教程和实践的记录,而UNITSTREAM包含流的记录(讲座)。
我也想要程序 引发一个名为CLASS_OVERLAPS的异常,在发生碰撞时打印一条警告消息。
我对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
BEGIN
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;
答案 0 :(得分:2)
您可以尝试这样:
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
BEGIN
DECLARE
x NUMBER:=0;
BEGIN
-- checks
SELECT nvl((SELECT 1 FROM TUTPRAC WHERE CLASSID = p_class and UNITCODE = p_unitc and CLASS_DAY = p_classd and CLASS_TIME = p_classt and CLASS_TYPE = p_classtp and ROOMNUM = p_roomnm) , 0) INTO x FROM dual;
-- insert
IF (x = 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;
或者你可以使用这样的EXISTS:
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
BEGIN
INSERT INTO TUTPRAC ("CLASSID", "UNITCODE", "CLASS_DAY", "CLASS_TIME", "CLASS_TYPE", "ROOMNUM")
INSERT INTO table
SELECT p_class, p_unitc, p_classd, p_classt, p_classtp, p_roomnm
FROM dual
WHERE NOT EXISTS (SELECT NULL
FROM TUTPRAC
WHERE CLASSID = p_class and UNITCODE = p_unitc and CLASS_DAY = p_classd and CLASS_TIME = p_classt and CLASS_TYPE = p_classtp and ROOMNUM = p_roomnm
)
答案 1 :(得分: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
BEGIN
merge into TUTPRAC a
using (select p_class CLASSID,
p_unitc UNITCODE,
p_classd CLASS_DAY,
p_classt CLASS_TIME,
p_classtp CLASS_TYPE,
p_roomnm ROOMNUM from dual) b
on (a.CLASSID = b.CLASSID
and a.UNITCODE = b.UNITCODE
and a.CLASS_DAY = b.CLASS_DAY
and a.CLASS_TYPE = b.CLASS_TYPE
and a.ROOMNUM = b.ROOMNUM)
WHEN NOT MATCHED THEN INSERT (a.CLASSID ,a.UNITCODE, a.CLASS_DAY, a.CLASS_TYPE, a.ROOMNUM)
values ( b.CLASSID
, b.UNITCODE
, b.CLASS_DAY
, b.CLASS_TYPE
, b.ROOMNUM);
if sql%ROWCOUNT = 0 then
dbms_output.put_line('Class alredy exists');
else
dbms_output.put_line('Class added');
end if;
commit;
END;
/