oracle触发器更新行

时间:2013-10-31 14:28:58

标签: oracle triggers

我有以下触发器:

create or replace 
trigger updateBeds
  After insert or update on INPATIENT_STAY for
    each row
Begin
if(:new.INPATIENT_STAY.DATEPLACEDINWARD<=sysdate and :new.INPATIENT_STAY.expectedleavedate>=sysdate)
then 
  update WARD
    set BEDSAVAIL = TOTALBEDS - 1
    where WARDNUM = :new.INPATIENT_STAY.WARDNUM;
end if;
if(:new.INPATIENT_STAY.DATELEFT <= sysdate) then
  update WARD
    set BEDSAVAIL = TOTALBEDS +1
    where WARDNUM = :new.INPATIENT_STAY.WARDNUM;
end if;
END;

如果totalbedsINPATIENT_STAY.DATEPLACEDINWARD小于系统日期,则此触发器旨在从列INPATIENT_STAY.expectedleavedate中减去1。 此外,如果INPATIENT_STAY.DATELEFT小于或等于系统日期,则totalbeds应增加1。 WARDINPATIENT_STAY中的INPATIENT_STAY

中的外键与WARDNUM相关联

当我尝试编译触发器时,我收到以下错误:

Error(2,4): PLS-00049: bad bind variable 'NEW.INPATIENT_STAY'
Error(2,54): PLS-00049: bad bind variable 'NEW.INPATIENT_STAY'
Error(6,21): PLS-00049: bad bind variable 'NEW.INPATIENT_STAY'
Error(8,4): PLS-00049: bad bind variable 'NEW.INPATIENT_STAY'
Error(11,21): PLS-00049: bad bind variable 'NEW.INPATIENT_STAY'

我不确定我在这里做错了什么。非常感谢任何帮助

表格是:

 WARD
    WARDNUM VARCHAR2(20 BYTE)
    NAME VARCHAR2(40 BYTE)
    TOTALBEDS NUMBER
    BEDSAVAIL NUMBER

INPATIENT_STAY
   INPATIENTID VARCHAR2(20 BYTE)
   WARDNUM VARCHAR2(20 BYTE)
   EXPECTEDLEAVEDATE DATE
   DATEPLACEDINWARD DATE
   DATELEFT DATE

1 个答案:

答案 0 :(得分:1)

要从插入/更新的行访问列的值,只需使用:new.column_name - 您不应在:new和列名称之间添加表名,请尝试以下操作:

create or replace 
trigger updateBeds
  After insert or update on INPATIENT_STAY for
    each row
Begin
if(:new.DATEPLACEDINWARD<=sysdate and :new.expectedleavedate>=sysdate)
then 
  update WARD
    set BEDSAVAIL = TOTALBEDS - 1
    where WARDNUM = :new.WARDNUM;
end if;
if(:new.DATELEFT <= sysdate) then
  update WARD
    set BEDSAVAIL = TOTALBEDS +1
    where WARDNUM = :new.WARDNUM;
end if;
END;