1

我是 sql 新手。我想创建一个用于在表之间复制值的触发器。基本上,我要完成的任务是将学生的消息表值转发到特定的 staff_maibox
这里是代码。

drop trigger forward_msg_to_staff;

create or replace trigger forward_msg_to_staff
update on message 
for each row
declare
  message_id    VARCHAR2(10);
  client_id     NUMBER(10);
  staff_id      NUMBER(5);
  message_date  DATE;
  message_title VARCHAR2(20);
  staff_mailbox VARCHAR2(255);
begin
  insert into staff_mailbox(message_id, client_id, staff_id, message_date, message_title, staff_mailbox)
    values(:new.message_id, :new.client_id, :new.staff_id, :sysdate, :new.message_title, :old.staff_mailbox)
end;
/

这段代码正确吗?请指教。提前致谢。

4

1 回答 1

3

您收到错误是因为您缺少CREATE TRIGGER 语句中的 BEFORE 或 AFTER 关键字中的 BEFORE 或 AFTER 关键字。

如文档中所述,这些是必需的:

在此处输入图像描述

此外:

  • 无需声明所有变量,您没有使用它们
  • :sysdate不正确,你没有绑定它。您可以sysdate像在标准 SQL 或 PL/SQL 中那样使用。
  • 在 INSERT 语句的 VALUES 子句之后缺少一个分号。

把它放在一起你的触发器可能看起来像这样

create or replace trigger forward_msg_to_staff
 after update on message 
 for each row
begin
  insert into staff_mailbox( message_id, client_id, staff_id, message_date 
                           , message_title, staff_mailbox )
  values ( :new.message_id, :new.client_id, :new.staff_id, sysdate
         , :new.message_title, :old.staff_mailbox );
end forward_msg_to_staff;
/

请注意,我也在 E​​ND 中使用了触发器名称。这只是为了方便起见,它使触发器结束的位置很明显......

如果您想查看在创建触发器时遇到的错误,请show errors按照a_horse_with_no_name 的建议使用。这显示了任何编译错误,这对于追踪它们是非常宝贵的。

于 2013-06-29T15:52:03.783 回答