Sql 带IF的Oracle插入触发器
我正试图触发,但出现了一个错误。 如果有人能向我解释我做错了什么,我将不胜感激。这是我大学期末项目的一部分Sql 带IF的Oracle插入触发器,sql,database,oracle,plsql,triggers,Sql,Database,Oracle,Plsql,Triggers,我正试图触发,但出现了一个错误。 如果有人能向我解释我做错了什么,我将不胜感激。这是我大学期末项目的一部分 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;
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;
你犯了一个错误这一事实并没有什么用处。我们怎么能猜出是哪一个呢 无论如何,我试着复习你写的代码;以下是明显错误的地方: 您声明了result char3-当心char数据类型,因为它用空格右键填充值,直到列的总长度。更安全的选择是使用VARCHAR2。但是,如果这些结果真的是长度始终为3个字符的字符串,那么可以使用它。 不是result=:new.result,而是result:=:new.result缺少冒号 如果:new.PUIID在SELECT PUIID FROM CASE中,则-此处有两个错误: 在此上下文中不能使用子查询。首先找出case表中是否存在:new.puiid,这意味着您必须声明额外的局部变量,然后在IF中使用它 即使您可以使用子查询,也不是如果:new.puiid在中,而是如果:new.puiid在不在中 不是elseif而是elseif 插入到箱子里是错误的。您指定了11个要插入的列,但提供了10个值。解决这个问题。 插入案例事件:您正在插入:new.date。日期列名称无效,因为它是为数据类型名称保留的。这似乎通常是错误的,除非在命名列时使用双引号,这同样是错误的,因为在创建对象时应该避免在Oracle中使用双引号 因为我没有你的桌子,我不能测试它。但是,如果将这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;
/