带if的oracle insert触发器

7d7tgy0s  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(588)

我试着扣动扳机,但出了个错误。如果有人能向我解释一下我做错了什么,我将不胜感激。这是我大学毕业设计的一部分。

CREATE OR REPLACE TRIGGER NewTest_CaseEvent_Trigger
AFTER
INSERT
ON TESTRESULTS
FOR EACH ROW
DECLARE
    NEW_CASEID        Integer;
    RESULT            Char(3);

BEGIN
    RESULT = :new.Result;

    IF :new.PUIID IS IN (SELECT PUIID FROM CASE)THEN
       SELECT CASEID INTO NEW_CASEID 
       FROM CASE
       WHERE PUIID = :new.PUIID;

       INSERT INTO  CASEEVENT
       (CASEEVENTID, CASEID, EventDate, EventComments, MethodOfCommunication, CreatedBy, CreationDate, LastUpdateBy, LastUpdateDate)
           VALUES
           (CASEEVENTID_PK.NextVal, New_CaseID, :new.Date, 'New Test Result Available: include here the test result',
               Null, 'NewTest_CaseEvent_Trigger',SYSDATE,Null,Null);

     ELSEIF RESULT = 'PST' THEN
         INSERT INTO CASE
         (CASEID, PUIID, ActivationDate, ClosingDate, ClosingReason, ExposureTypeID, CaseWorkerID, CreatedBy,CreationDate, LastUpdateBy, LastUpdateDate)
         VALUES
         (CASEID_PK.NextVal, :new.PUIID, SYSDATE,NUll,NUll,NUll,'NewTest_CaseEvent_Trigger',SYSDATE,NUll,NUll);

          INSERT INTO  CASEEVENT
       (CASEEVENTID, CASEID, EventDate, EventComments, MethodOfCommunication, CreatedBy, CreationDate, LastUpdateBy, LastUpdateDate)
           VALUES
           (CASEEVENTID_PK.NextVal, CASEID_PK.CurrVal, :new.Date, 'New Test Result Available: include here the test result',
               Null, 'NewTest_CaseEvent_Trigger',SYSDATE,Null,Null);
     END IF;
END;
mwyxok5s

mwyxok5s1#

你犯了一个错误这一事实没有多大用处。我们怎么猜是哪一个?
不管怎样,我试着复习你写的代码;下面是明显的错误:
你宣布 result char(3) -当心 char 数据类型右键填充值,空格最大为总列长度。更安全的选择是使用 VARCHAR2 . 但是,如果这些结果真的是长度始终为3个字符的字符串,那么可以使用它。
不是 result = :new.result ,但是 result := :new.result (缺少冒号) IF :new.PUIID IS IN (SELECT PUIID FROM CASE) THEN -这里有两个错误:
不能在此上下文中使用子查询。先弄清楚 :new.puiid 存在于 case 表(这意味着您必须声明额外的局部变量),然后在中使用它 IF 即使可以使用子查询,也不是 if :new.puiid IS in 但是 if :new.puiid in (不带“是”)
不是的 elseif 但是
elsif insert into case 这是错误的。您指定了11个要插入的列,但提供了10个值。修好它。 insert into caseevent :您正在插入 :new.date . date 列名称无效,因为它是为数据类型名称保留的。这似乎通常是错误的,除非在命名列时使用双引号,而且这也是错误的,因为在创建对象时应该避免在oracle中使用双引号
因为我没有你的table,我无法测试。但是,下面的代码可能会起作用(如果您修复了“10个值分成11列”的问题)。

create or replace trigger newtest_caseevent_trigger 
  after insert on testresults
  for each row 
declare
  new_caseid  integer;
  result      char(3);           -- beware of CHAR!
  l_puuid     case.puiid%type;   -- newly declared variable
begin 
  result := :new.result;         --  add ":"

  -- subquery can't be used in IF, so you'll have to find whether :new.puiid
  -- exists in that table separately
  select max(puiid) into l_puiid from case where puiid = :new.puiid;

  if l_puiid is not null then
     select caseid 
       into new_caseid 
       from case
       where puiid = :new.puiid;

     insert into caseevent
       (caseeventid, caseid, eventdate, 
        eventcomments, 
        methodofcommunication, createdby, creationdate, 
        lastupdateby, lastupdatedate)
     values
       (caseeventid_pk.nextval, new_caseid, :new.date, 
        'New Test Result Available: include here the test result',
        null, 'NewTest_CaseEvent_Trigger',sysdate,
        null,null);

   elsif result = 'PST' then    -- "elsif", not "elseif"
      -- you are inserting 10 values into 11 columns; that won't work
      insert into case
        (caseid, puiid, activationdate, closingdate, 
         closingreason, exposuretypeid, caseworkerid, createdby,
         creationdate, lastupdateby, lastupdatedate)
      values
        (caseid_pk.nextval, :new.puiid, sysdate, null,
         null, null, 'NewTest_CaseEvent_Trigger', sysdate,
         null, null);

      insert into caseevent
        (caseeventid, caseid, eventdate, 
         eventcomments, 
         methodofcommunication, createdby, creationdate, lastupdateby, lastupdatedate)
      values
        (caseeventid_pk.nextval, caseid_pk.currval, :new.date,
         'New Test Result Available: include here the test result',
         null, 'NewTest_CaseEvent_Trigger', sysdate, null, null); 
  end if;
end;
/

相关问题