我正在尝试运行此代码来为我的审计表创建一个触发器,但是我收到了错误消息:
PLS-00103: Encountered the symbol "TRG_AUDIT_EMP" when expecting one of the following: if
如何解决此问题?
create or replace TRIGGER trg_audit_emp
AFTER INSERT OR DELETE OR UPDATE ON emp_mb
FOR EACH ROW
DECLARE
v_trg_action VARCHAR2(6);
BEGIN
IF updating
THEN
v_trg_action := 'UPDATE';
ELSE IF deleting
THEN
v_trg_action := 'DELETE';
ELSE IF inserting
THEN
v_trg_action := 'INSERT';
ELSE
v_trg_action := 'NULL';
END IF;
IF v_trg_action IN ('DELETE','UPDATE') THEN
INSERT INTO emp_audit
( emp_id, emp_forename, emp_surname, username, AUDIT_USER, AUDIT_DATE, AUDIT_ACTION)
VALUES
(:OLD.emp_ID, :OLD.emp_forename, :OLD.emp_surname, :old.username, UPPER(v('APP_USER')), SYSDATE, v_trg_action);
ELSE
INSERT INTO emp_audit
( emp_id, emp_forename, emp_surname, username, AUDIT_USER, AUDIT_DATE, AUDIT_ACTION)
VALUES
(:NEW.emp_id, :NEW.emp_forename, :NEW.emp_surname, NEW.username, UPPER(v('APP_USER')), SYSDATE, v_trg_action);
END IF;
END trg_audit_emp ;
答案 0 :(得分:0)
在trg_audit_emp
语法后删除END
!而且你需要结束你的ELSE IF
示例:
create or replace TRIGGER trg_audit_emp
AFTER INSERT OR DELETE OR UPDATE ON emp_mb
FOR EACH ROW
DECLARE
v_trg_action VARCHAR2(6);
BEGIN
IF updating
THEN
v_trg_action := 'UPDATE';
ELSE IF deleting
THEN
v_trg_action := 'DELETE';
ELSE IF inserting
THEN
v_trg_action := 'INSERT';
ELSE
v_trg_action := 'NULL';
END IF;
END IF;
END IF;
IF v_trg_action IN ('DELETE','UPDATE') THEN
INSERT INTO emp_audit
( emp_id, emp_forename, emp_surname, username, AUDIT_USER, AUDIT_DATE, AUDIT_ACTION)
VALUES
(:OLD.emp_ID, :OLD.emp_forename, :OLD.emp_surname, :old.username, UPPER(v('APP_USER')), SYSDATE, v_trg_action);
ELSE
INSERT INTO emp_audit
( emp_id, emp_forename, emp_surname, username, AUDIT_USER, AUDIT_DATE, AUDIT_ACTION)
VALUES
(:NEW.emp_id, :NEW.emp_forename, :NEW.emp_surname, NEW.username, UPPER(v('APP_USER')), SYSDATE, v_trg_action);
END IF;
END;
/
答案 1 :(得分:0)
在PL / SQL中,IF语句的语法是
IF condition THEN
statement
ELSIF condition THEN
statement
ELSE
statement
END;
您有ELSE IF
而不是ELSIF
,然后END IF
太多了。
试试这个:
create or replace TRIGGER trg_audit_emp
AFTER INSERT OR DELETE OR UPDATE ON emp_mb
FOR EACH ROW
DECLARE
v_trg_action VARCHAR2(6);
BEGIN
IF updating THEN
v_trg_action := 'UPDATE';
ELSIF deleting THEN
v_trg_action := 'DELETE';
ELSIF inserting THEN
v_trg_action := 'INSERT';
ELSE
v_trg_action := 'NULL';
END IF;
IF v_trg_action IN ('DELETE','UPDATE') THEN
INSERT INTO emp_audit (emp_id, emp_forename, emp_surname,username,
AUDIT_USER, AUDIT_DATE, AUDIT_ACTION)
VALUES (:OLD.emp_ID, :OLD.emp_forename, :OLD.emp_surname, :old.username,
UPPER(v('APP_USER')), SYSDATE, v_trg_action);
ELSE
INSERT INTO emp_audit (emp_id, emp_forename, emp_surname, username,
AUDIT_USER, AUDIT_DATE, AUDIT_ACTION)
VALUES (:NEW.emp_id, :NEW.emp_forename, :NEW.emp_surname, NEW.username,
UPPER(v('APP_USER')), SYSDATE, v_trg_action);
END IF;
END trg_audit_emp;
分享并享受。