[英]Oracle SQL Trigger partially not working
I am writing a database script for an assignment. 我正在为任务编写数据库脚本。 I have a trigger that I want to create employment history from updates on Employees table. 我有一个触发器,我想根据“雇员”表上的更新来创建雇佣历史记录。 It works for changing the salary, but does not when changing job_title, to either null
of different value. 它适用于更改薪水,但不适用于将job_title更改为其他值的null
的情况。
create or replace trigger employment_update
after update on employees for each row
begin
if :old.salary <> :new.salary then
insert into employment_history values (empl_hist_seq.nextval,sysdate,'zmiana_wynagrodzenia',:new.employee_id);
end if;
if :new.job_title = null then
insert into employment_history values (empl_hist_seq.nextval,sysdate,'zwolnienie',:new.employee_id);
elsif :old.job_title <> :new.job_title then
insert into employment_history values (empl_hist_seq.nextval,sysdate,'zmiana_stanowiska',:new.employee_id);
end if;
end;
/
Testing: 测试:
update employees set job_title = null where employee_id = 2;
update employees set job_title = 'test' where employee_id = 2;
update employees set salary = 1500 where employee_id = 4;
select * from EMPLOYMENT_HISTORY;
show errors;
................................................................................
1 row updated.
1 row updated.
1 row updated.
HISTORY_ID CHANGE_DATE CHANGE_TYPE EMPLOYEE_ID
---------- ----------- -------------------------------- -----------
0 15/11/26 zatrudnienie 0
1 15/11/26 zatrudnienie 1
2 15/11/26 zatrudnienie 2
3 15/11/26 zatrudnienie 3
4 15/11/26 zatrudnienie 4
5 15/11/26 zatrudnienie 5
6 15/11/26 zatrudnienie 6
7 15/11/26 zatrudnienie 7
8 15/11/26 zatrudnienie 8
9 15/11/26 zatrudnienie 9
10 15/11/26 zmiana_wynagrodzenia 4
11 rows selected
No errors.
Tables: 表格:
employees 雇员
create table employees
(
employee_id number(5) primary key,
first_name varchar2(16) not null,
last_name varchar2(32) not null,
job_title varchar2(32),
salary number(8,2),
address_id number(5) references addresses(address_id) not null
);
employment_history + sequence 就业历史+顺序
create sequence empl_hist_seq
increment by 1
minvalue 0
maxvalue 99999
nocycle
nocache;
create table employment_history
(
history_id number(5) primary key,
change_date date not null,
change_type varchar2(32) not null,
employee_id number(3) references employees(employee_id) not null
);
There is one immediate obvious error: 有一个直接的明显错误:
if :new.job_title = null then
You don't use = null
you should use is null
您不使用= null
,应该使用is null
if :new.job_title is null then
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.