尝试创建触发器时出错(忽略无效的标识符和sql)

时间:2017-05-06 01:57:18

标签: sql oracle plsql triggers

我在酒店数据库中工作。我正在尝试创建一个触发器,根据当前日期是否在房间的check_in和check_out值之间进行任何新预订,将更改房间的可用性状态。

我的编译器日志中有2个错误:

  

对于每一行'

,忽略了SQL语句

  

ora00904:" status_in":无效标识符

我将不胜感激。

表:

create table room(
    room_id number(3) constraint room_pk primary key,
    room_type varchar2(15) constraint room_fk references roomType,
    status char(1)
);

create table reservation(
    reservation_id number(6) constraint reservation_pk primary key,
    check_in date,
    check_out date, 
    room_id number(3),
    guest_id varchar2(5),
    foreign key (room_id) references room(room_id),
    foreign key (guest_id) references guest(guest_id)
);

触发:

create or replace trigger room_status
before insert on reservation
for each row
declare
    status_in room.status%type;
    error1 exception;
begin

    select status
      into status_in
      from room
      where room_id = :old.room_id;

    if sysdate between :old.check_in and :old.check_out then
      update room
       set status_in = 'F'
       where room_id = :old.room_id;
    else
       update room
         set status_in = 'T'
         where room_id = :old.room_id;
    end if;

exception
  when error1 then
    raise_application_error(-20100,'Insert Cancelled');
end;

1 个答案:

答案 0 :(得分:1)

update语句尝试更新status_in的{​​{1}}列:

room

update room set status_in = 'F' 没有该列。它有一个room列。

我也不确定你对检索旧房间状态的意图是什么。你打算用它来检查一下吗?如果没有,你的触发器是否会做这样的事情:

status

如果是这样,那么您可能也会怀疑您是否真的想要update room set status = (sysdate between :old.check_in and :old.check_out) where room_id = :old.room_id ; 表上的status列。 room实际上是派生列。设置status而不是计算它将使您面临不一致的危险。相反,你可以沿着这些方向做点什么:

status

查询:

create table room(
    room_id number(3) constraint room_pk primary key,
    room_type varchar2(15) constraint room_fk references roomType
);