oracle 如何编写PL/SQL以在插入之前首先检查记录是否存在

r1wp621o  于 2023-04-29  发布在  Oracle
关注(0)|答案(3)|浏览(154)

***背景:***我有表格:
**TUTPRAC:**CLASSID,UNITCODE,STAFFNO,CLASSDAY,CLASSTIME,CLASSTYPE,ROOMNUM
**UNITSTREAM:**STREAM、UNITCODE、STAFFNO、DAY、TIME、LOCATION
***问题:***所以我有一个数据库和表,我试图创建一个函数,在打开一个新的类之前检查一个类是否打开,这个过程必须确保流(讲座),tute会话和prac类之间没有任何重叠(在时间-天或位置方面的冲突)。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;
7vhp5slm

7vhp5slm1#

你可以这样尝试:

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
                  )
btxsgosb

btxsgosb2#

你知道合并吗?合并

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;
/
gdrx4gfi

gdrx4gfi3#

保持简单

这里有一个更简单的方法:

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") 
  SELECT p_class, p_unitc, p_classd, p_classt, p_classtp, p_roomnm
  FROM   DUAL
  WHERE (SELECT COUNT(*) 
         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;

  COMMIT;

END;

相关问题