简体   繁体   English

如果触发在列上插入值?

[英]Trigger to insert Value on a column if?

I need to create a trigger on a table that updates in every insert the value of a column if a condition is present. 我需要在表上创建一个触发器,如果​​存在条件,则在每个插入中更新列的值。 So in table like this: 所以在这样的表中:

在此输入图像描述

I need the trigger to update all null values to '1' if "RegimeIva" = '3' in order to get a result like this: 如果“RegimeIva”='3'我需要触发器将所有空值更新为'1'以获得如下结果:

在此输入图像描述

I have tried: 我努力了:

create or replace TRIGGER AtualizaNat before INSERT ON Cabecdoc  
for each row
BEGIN
  if :new.IntrastatnatA is null and :new.intrastatnatB  is null and :new.regimeiva = '3' then
    :new.intrastatnatA : 1; :new.intrastatnatB : 1;
  else
    :new.intrastatnatA : null; :new.intrastatnatB : null; 
  end if;
END;

This is not working 这不起作用

Corrected few syntax errors in your code. 更正了代码中的一些语法错误。

Here's a little example. 这是一个小例子。 It works fine for me: 这对我来说可以:

create table Cabecdoc (IntrastatnatA number, IntrastatnatB number, regimeiva char(1));


create or replace TRIGGER AtualizaNat before INSERT ON Cabecdoc  
for each row
BEGIN
  if :new.IntrastatnatA is null and :new.intrastatnatB  is null and :new.regimeiva = '3' then
    :new.intrastatnatA := 1; :new.intrastatnatB := 1;
  else
    :new.intrastatnatA := null; :new.intrastatnatB := null;
  end if;
END;

--inserting a row...
insert into Cabecdoc values (null, null, '3');

--...and checking
select * from Cabecdoc;


--Output:
IntrastatnatA IntrastatnatB regimeiva
1             1             3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM