在表上创建触发器以在列中插入值

时间:2015-07-14 16:08:33

标签: oracle plsql triggers

如何创建触发器以在列中插入值,具体取决于表中新插入行的同一表的3个其他列的组合。

CREATE OR REPLACE TRIGGER EXECUTION_TRIGGER 

AFTER INSERT ON JOBS  
DECLARE 
V_A varchar2(100);
V_B varchar2(100);
V_C VARCHAR2(120);

BEGIN

select CATEGORY, METHOD,TECH  into V_A, V_B, V_C FROM JOBS

IF  V_C = '10X' AND V_B = '20X' AND V_A = '30X'

THEN

UPDATE JOBS 
SET EXECUTION = 1;

ELSE 

UPDATE JOBS 
SET EXECUTION = 2;


END IF ;
  NULL;
END

我无法执行此操作......

1 个答案:

答案 0 :(得分:0)

您需要BEFORE INSERT触发器:

create or replace TRIGGER EXECUTION_TRIGGER before INSERT ON JOBS  
for each row
BEGIN
  if :new.CATEGORY = '30X' and :new.METHOD = '20X' and :new.TECH = '10X' then
    :new.execution := 1; 
  else
    :new.execution := 2; 
  end if;
END;

测试:

create table jobs (CATEGORY varchar2(10), METHOD    varchar2(10), 
                  TECH      varchar2(10), execution number(5));

insert into jobs (CATEGORY, METHOD, TECH) values ('30X', '20X', '10X');
insert into jobs (CATEGORY, METHOD, TECH) values ('abc', 'def', 'ghi');

select * from jobs;

CATEGORY   METHOD     TECH        EXECUTION
---------- ---------- ---------- ----------
30X        20X        10X                 1 
abc        def        ghi                 2