我一直收到以下错误,我无法弄清楚原因。我已经在代码中包含了表格,然后是表示错误的代码部分。
代码中的表:
CREATE TABLE Employee (
emp_id number(8) primary key,
f_name VarChar2(20),
l_name VarChar2(20),
address VarChar2(40),
city VarChar2(20),
state Char(2),
zip Number(5),
phone_Number Number(10),
email_address VarChar2(30),
dept_id number(8) references department(Dept_ID),
office_location VarChar2(30)
);
CREATE TABLE audit_detail (
audit_detail_id number(8) primary key,
field varchar2(30),
old_value varchar2(30),
new_value varchar2(30)
);
CREATE TABLE audit_trail (
audit_trail_id number(8) primary key,
user_id number(8)references user_id_table(user_id),
table_name VarChar2(25),
process VarChar2(25),
emp_id number(8)references employee(emp_id),
timestamp Date
);
Create table Department (
dept_id number(8) primary key,
dept_name VarChar2(20) not null,
dept_start_date date
);
create table user_id_table(
user_id number(8) primary key,
emp_id number(8) references Employee(emp_id)
);
CREATE SEQUENCE audit_seq;
这是出现错误的部分,特别是elsif删除部分
CREATE OR REPLACE TRIGGER audit_employee
AFTER INSERT OR UPDATE OR DELETE ON Employee
FOR EACH ROW
DECLARE
timestamp DATE;
session_user number(8);
BEGIN
timestamp := SYSDATE;
session_user := USERENV('SESSION_USER');
IF INSERTING THEN
INSERT INTO audit_trail VALUES
(Audit_seq.NEXTVAL, session_user, 'Employee', 'INSERT', :new.emp_id, timestamp);
ElSIF DELETING THEN
INSERT INTO audit_trail Values
(Audit_seq.NEXTVAL, session_user, 'Employee', 'DELETE', :old.emp_id, timestamp);
Else
INSERT INTO audit_trail
Values(Audit_seq.NEXTVAL, session_user, 'Employee', 'UPDATE', :old.emp_id);
IF UPDATING ('f_name') THEN
INSERT INTO audit_detail VALUES
(Audit_seq.CURRVAL, 'f_name', :old.f_name, :new.f_name);
ELSIF UPDATING('l_name') THEN
INSERT INTO audit_detail VALUES
(Audit_seq.CURRVAL, 'l_name', :old.l_name, :new.l_name);
ELSIF UPDATING('address') THEN
INSERT INTO audit_detail VALUES
(Audit_seq.CURRVAL, 'address', :old.address, :new.address);
ELSIF UPDATING('city') THEN
INSERT INTO audit_detail VALUES
(Audit_seq.CURRVAL, 'city', :old.city, :new.city);
ELSIF UPDATING('state') THEN
INSERT INTO audit_detail VALUES
(Audit_seq.CURRVAL, 'state', :old.state, :new.state);
ELSIF UPDATING('zip') THEN
INSERT INTO audit_detail VALUES
(Audit_seq.CURRVAL, 'zip', :old.zip, :new.zip);
ELSIF UPDATING('phone_Number') THEN
INSERT INTO Audit_detail VALUES
(Audit_seq.CURRVAL, 'phone_Number', :old.phone_number, :new.phone_number);
ELSIF UPDATING('email_address') THEN
INSERT INTO audit_detail VALUES
(Audit_seq.CURRVAL, 'email_address', :old.email_address, :new.email_address);
ELSIF UPDATING('dept_id') THEN
INSERT INTO audit_detail VALUES
(Audit_seq.CURRVAL, 'dept_id', :old.dept_id, :new.dept_id);
ELSIF UPDATING('office_location') THEN
INSERT INTO audit_detail VALUES
(Audit_seq.CURRVAL, 'office_location', :old.office_location, :new.office_location);
End IF;
End IF;
End;
/
答案 0 :(得分:1)
删除后的else语句有5列,而audit_trail有6列。
ElSIF DELETING THEN
INSERT INTO audit_trail
Values ( Audit_seq.NEXTVAL, session_user, 'Employee'
, 'DELETE', :old.emp_id, timestamp);
Else
INSERT INTO audit_trail
Values (Audit_seq.NEXTVAL, session_user, 'Employee'
, 'UPDATE', :old.emp_id );
您还引用了小写而不是UPDATING ('L_NAME')
的小写列。我不确定这是否会导致问题。
你正在做很多隐含的数字到字符的转换,最终会把你赶出去,而audit_trail中的table_name应该是30个字符而不是25个。为什么要冒险呢?