PLS-00049 BAD BIND VARIABLE [closed]

2019-03-06 20:29发布

I'm trying to set up a trigger so that whenever the PL_Witness table is updated, it makes a record of this in the PLAUDWIT table which is an auditing table.

However, every single time I try to make this trigger I get bad bind variable, and I am getting this on other audit triggers I am attempting to make too. What is my common issue?

All Help is appreciated!

CREATE TABLE  "PL_WITNESS" 
   (    "WITNESS_ID" NUMBER(*,0) NOT NULL ENABLE, 
    "WITNESS_NAME" VARCHAR2(30) NOT NULL ENABLE, 
    "WITNESS_ADDRESS" VARCHAR2(100), 
    "FK1_WITNESS_TYPE_ID" NUMBER(*,0) NOT NULL ENABLE, 
     CONSTRAINT "PK_WITNESS" PRIMARY KEY ("WITNESS_ID") ENABLE
   )
/
ALTER TABLE  "PL_WITNESS" ADD CONSTRAINT "FK1_WITNESS_WTYPE" FOREIGN KEY         ("FK1_WITNESS_TYPE_ID")
      REFERENCES  "PL_WITNESS_TYPE" ("WITNESS_TYPE_ID") ENABLE
/

.

DROP TABLE PLAUDWIT
CREATE TABLE PLAUDWIT (
AUD_AWitnessID NUMBER,
AUD_AWitnessType NUMBER, 
AUDIT_USER varchar2(50),
AUDIT_DATE DATE,
AUDIT_ACTION varchar2(10));

. CREATE OR REPLACE TRIGGER TRG_PLAUDWIT

AFTER INSERT OR DELETE OR UPDATE ON PL_WITNESS
FOR EACH ROW
DECLARE
v_trigger_task varchar2(10);

BEGIN
IF UPDATING
  THEN
    v_trigger_task := 'Update';
ELSIF DELETING
THEN
v_trigger_task := 'DELETE';
ELSIF INSERTING
THEN
v_trigger_task := 'INSERT';
ELSE
v_trigger_task := NULL;
END IF;

IF v_trigger_task IN ('DELETE','UPDATE') THEN
INSERT INTO PLAUDWIT (AWitnessID, AWitnessType, AUDIT_USER, AUDIT_DATE, AUDIT_ACTION)
VALUES
(:OLD.AWitnessID, :OLD.AWitnessType, UPPER(v('APP USER')), SYSDATE, v_trigger_task);
ELSE

INSERT INTO PLAUDWIT (AWitnessID, AWitnessType, AUDIT_USER, AUDIT_DATE, AUDIT_ACTION)
VALUES
(:NEW.AWitnessID, :NEW.AWitnessType, UPPER(v('APP USER')), SYSDATE, v_trigger_task);
END IF;

END TRG_PLAUDWIT;

1条回答
叼着烟拽天下
2楼-- · 2019-03-06 21:00

You're referring to bind varibales with an 'A' at the start and no underscore, like :OLD.AWitnessID, but your table column is just WITNESS_ID. So they don't match, and generate this error. You don't even have a WITNESS_TYPE column.

Then in your insert statements you have the column names in the audit table wrong too. You also set the variable to Update but check for UPDATE - remmeber the comparison is case-sensitive for string values.

This compiles with your schema:

CREATE OR REPLACE TRIGGER TRG_PLAUDWIT
AFTER INSERT OR DELETE OR UPDATE ON PL_WITNESS
FOR EACH ROW
DECLARE
  v_trigger_task varchar2(10);
BEGIN
  IF UPDATING THEN
    v_trigger_task := 'UPDATE';
  ELSIF DELETING THEN
    v_trigger_task := 'DELETE';
  ELSIF INSERTING THEN
    v_trigger_task := 'INSERT';
  ELSE
    v_trigger_task := NULL;
  END IF;

  IF v_trigger_task IN ('DELETE','UPDATE') THEN
    INSERT INTO PLAUDWIT (AUD_AWitnessID, AUD_AWitnessType, AUDIT_USER,
      AUDIT_DATE, AUDIT_ACTION)
    VALUES (:OLD.Witness_ID, :OLD.FK1_WITNESS_TYPE_ID, UPPER(v('APP USER')),
      SYSDATE, v_trigger_task);
  ELSE    
    INSERT INTO PLAUDWIT (AUD_AWitnessID, AUD_AWitnessType, AUDIT_USER,
      AUDIT_DATE, AUDIT_ACTION)
    VALUES (:NEW.Witness_ID, :NEW.FK1_WITNESS_TYPE_ID, UPPER(v('APP USER')),
      SYSDATE, v_trigger_task);
  END IF;
END TRG_PLAUDWIT;
/

SQL Fiddle showing no compilation errors.

查看更多
登录 后发表回答