触发要在更新属性上执行?

发布于 2024-12-18 13:55:32 字数 998 浏览 1 评论 0原文

我的触发器有问题,但我找不到原因。

这些是表:

create table Properties(  
    idProperties number(10) NOT NULL , 
    Type varchar2(45) NOT NULL,
    ConstructionDate date NOT NULL,
    FloorLocation varchar(20),
    Balkony varchar2(10),
    Price number(10) NOT NULL,
    DateOfInsert date NOT NULL,
    DateOfExiration date NOT NULL,
    Address_FK number(20),
    PropertyType_FK number(20) NOT NULL,
    Service_FK number(20),
    Ownership_FK number(20),
    PropertyService_FK number(20))

create table Services(
    idServices number(10) NOT NULL,
    servicetype varchar2(20))

我正在尝试编写一个触发器,当状态字段从“新”更改为“已批准”时,在更新 PROPERTY 时执行,然后用今天的日期更新到期日期系统日期 + 90 天

我正在尝试此操作,但它不起作用:

CREATE OR REPLACE TRIGGER T22
AFTER UPDATE ON Properties
FOR EACH ROW

Begin
  if :new.servicetype = 'APROVED' then
 :new.Servicetype := SYSDATE + 90;

end if;
End;

我需要使用属性表中的外键调用 servicetype 吗?或者这是正确的,但我错过了一些东西?

I have a problem with triggers and I can't find why.

These are the tables:

create table Properties(  
    idProperties number(10) NOT NULL , 
    Type varchar2(45) NOT NULL,
    ConstructionDate date NOT NULL,
    FloorLocation varchar(20),
    Balkony varchar2(10),
    Price number(10) NOT NULL,
    DateOfInsert date NOT NULL,
    DateOfExiration date NOT NULL,
    Address_FK number(20),
    PropertyType_FK number(20) NOT NULL,
    Service_FK number(20),
    Ownership_FK number(20),
    PropertyService_FK number(20))

create table Services(
    idServices number(10) NOT NULL,
    servicetype varchar2(20))

I am trying write a trigger to be executed on update of a PROPERTY when the status field is changed from “NEW” to “APPROVED” then update expiration date with today’s date Sysdate + 90 days.

I am trying this but it doesn't work:

CREATE OR REPLACE TRIGGER T22
AFTER UPDATE ON Properties
FOR EACH ROW

Begin
  if :new.servicetype = 'APROVED' then
 :new.Servicetype := SYSDATE + 90;

end if;
End;

Do I need to call servicetype with foreign key from the properties table? Or is this right but I am missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凝望流年 2024-12-25 13:55:32

您无法更改 AFTER 触发器中的值。您需要使用 BEFORE 触发器。

如果您使用的是 Oracle 11.2,并且尝试定义更改值的 AFTER 触发器,则应该收到“ORA-04084:无法更改此触发器类型的新值”错误

SQL> create table t (
  2    col1 number,
  3    col2 date
  4  );

Table created.

SQL> create trigger trg_t
  2    after update on t
  3    for each row
  4  begin
  5    :new.col2 := sysdate + 90;
  6  end;
  7  /
create trigger trg_t
               *
ERROR at line 1:
ORA-04084: cannot change NEW values for this trigger type

。 ,如果您将触发器创建为 BEFORE UPDATE 触发器,它应该可以工作

SQL> ed
Wrote file afiedt.buf

  1  create or replace trigger trg_t
  2    before update on t
  3    for each row
  4  begin
  5    :new.col2 := sysdate + 90;
  6* end;
SQL> /

Trigger created.

You cannot change a value in an AFTER trigger. You would need to use a BEFORE trigger.

If you're using Oracle 11.2, you should get an "ORA-04084: cannot change NEW values for this trigger type" error if you try to define an AFTER trigger that changes a value

SQL> create table t (
  2    col1 number,
  3    col2 date
  4  );

Table created.

SQL> create trigger trg_t
  2    after update on t
  3    for each row
  4  begin
  5    :new.col2 := sysdate + 90;
  6  end;
  7  /
create trigger trg_t
               *
ERROR at line 1:
ORA-04084: cannot change NEW values for this trigger type

On the other hand, if you create the trigger as a BEFORE UPDATE trigger, it should work

SQL> ed
Wrote file afiedt.buf

  1  create or replace trigger trg_t
  2    before update on t
  3    for each row
  4  begin
  5    :new.col2 := sysdate + 90;
  6* end;
SQL> /

Trigger created.
浅紫色的梦幻 2024-12-25 13:55:32

您必须从服务类型表中进行选择才能查看实际的服务描述,以便您可以比较之前和之后的描述。

此外,当您更改要插入的值时,您必须将其更改为更新前触发器。

SQL> select * from properties;

        ID  SERVICEID EXPIRATIO
---------- ---------- ---------
       100          1 26-FEB-12
       200          2 28-NOV-11

SQL> select * from services;

 SERVICEID SERVICETYPE
---------- --------------------
         1 APPROVED
         2 NEW

触发器定义:

create or replace trigger trg1
  before update on properties
  for each row
declare
  l_old_servicetype services.servicetype%type;
  l_new_servicetype services.servicetype%type;
begin
  dbms_output.put_line('changing from ');

  select servicetype
    into l_old_servicetype
    from services
    where ServiceId = :old.serviceId;

  select servicetype
    into l_new_servicetype
    from services
    where ServiceId = :new.serviceId;

  dbms_output.put_line('old value : ' || l_old_servicetype );
  dbms_output.put_line('nwe value : ' || l_new_servicetype );


   if( l_old_servicetype = 'NEW' and l_new_servicetype='APPROVED') then
      :new.expirationDate := SYSDATE + 90;
   end if; 

end;
/

测试:

SQL> select * from properties;

        ID  SERVICEID EXPIRATIO
---------- ---------- ---------
       100          2 28-NOV-11
       200          2 28-NOV-11

SQL> update properties set serviceId = 1 where id = 100;
changing from
old value : NEW
nwe value : APPROVED

1 row updated.

SQL> select * from properties;

        ID  SERVICEID EXPIRATIO
---------- ---------- ---------
       100          1 26-FEB-12
       200          2 28-NOV-11

You'll have to select from the service type table to see the actual service description so that you can compare the before and after descriptions.

Also, you'll have to change this to a before update trigger as you are changing the values that are about to be inserted.

SQL> select * from properties;

        ID  SERVICEID EXPIRATIO
---------- ---------- ---------
       100          1 26-FEB-12
       200          2 28-NOV-11

SQL> select * from services;

 SERVICEID SERVICETYPE
---------- --------------------
         1 APPROVED
         2 NEW

Trigger definition :

create or replace trigger trg1
  before update on properties
  for each row
declare
  l_old_servicetype services.servicetype%type;
  l_new_servicetype services.servicetype%type;
begin
  dbms_output.put_line('changing from ');

  select servicetype
    into l_old_servicetype
    from services
    where ServiceId = :old.serviceId;

  select servicetype
    into l_new_servicetype
    from services
    where ServiceId = :new.serviceId;

  dbms_output.put_line('old value : ' || l_old_servicetype );
  dbms_output.put_line('nwe value : ' || l_new_servicetype );


   if( l_old_servicetype = 'NEW' and l_new_servicetype='APPROVED') then
      :new.expirationDate := SYSDATE + 90;
   end if; 

end;
/

Testing :

SQL> select * from properties;

        ID  SERVICEID EXPIRATIO
---------- ---------- ---------
       100          2 28-NOV-11
       200          2 28-NOV-11

SQL> update properties set serviceId = 1 where id = 100;
changing from
old value : NEW
nwe value : APPROVED

1 row updated.

SQL> select * from properties;

        ID  SERVICEID EXPIRATIO
---------- ---------- ---------
       100          1 26-FEB-12
       200          2 28-NOV-11
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文