我的触发器出了什么问题?

时间:2017-10-10 13:43:39

标签: database oracle triggers

我正在使用Oracle 12.2c,我需要执行一个触发器:

审核表

CREATE TABLE "MY_USER"."AUDIT"
(
  "TABLE_NAME"  VARCHAR2(20 CHAR)       NOT NULL ENABLE,
  "REGISTER_ID" NUMBER(19, 0) DEFAULT 0 NOT NULL ENABLE,
  "OPERATION"   VARCHAR2(20 CHAR)       NOT NULL ENABLE,
  "DATETIME"    TIMESTAMP(6)            NOT NULL ENABLE,
  "PO_DOCUMENT" CLOB,
  CONSTRAINT "ENSURE_JSON" CHECK (po_document IS JSON) ENABLE
);

触发

CREATE OR REPLACE EDITIONABLE TRIGGER "MY_USER"."TRG_PARTNER"
AFTER DELETE OR INSERT OR UPDATE ON "MY_USER"."PARTNER"
FOR EACH ROW
DECLARE
  vOperation VARCHAR2(6);
  vTableName VARCHAR2(30) := 'PARTNER';
  vLOGON_TYPE CHAR(1);
  vLAST_UPDATE DATE;
  vSTATUS CHAR(1);
  vADMIN_PASSWORD VARCHAR2(255 CHAR);
  vPASSWORD VARCHAR2(255 CHAR);
  vSESSION_VALID_SECONDS NUMBER(10, 0);
  vONBOARDING CHAR(1);
  vID VARCHAR2(255 CHAR);
  vNAME VARCHAR2(255 CHAR);
  vNETWORK_CODE VARCHAR2(255 CHAR);

BEGIN

  IF INSERTING THEN
    vOperation := 'INSERT';
    vLOGON_TYPE := :NEW.LOGON_TYPE;
    vLAST_UPDATE := :NEW.LAST_UPDATE;
    vSTATUS := :NEW.STATUS;
    vADMIN_PASSWORD := :NEW.ADMIN_PASSWORD;
    vPASSWORD := :NEW.PASSWORD;
    vSESSION_VALID_SECONDS := :NEW.SESSION_VALID_SECONDS;
    vONBOARDING := :NEW.ONBOARDING;
    vID := :NEW.ID;
    vNAME := :NEW.NAME;
    vNETWORK_CODE := :NEW.NETWORK_CODE;

  ELSIF UPDATING THEN
    vOperation := 'UPDATE';
    vLOGON_TYPE := :NEW.LOGON_TYPE;
    vLAST_UPDATE := :NEW.LAST_UPDATE;
    vSTATUS := :NEW.STATUS;
    vADMIN_PASSWORD := :NEW.ADMIN_PASSWORD;
    vPASSWORD := :NEW.PASSWORD;
    vSESSION_VALID_SECONDS := :NEW.SESSION_VALID_SECONDS;
    vONBOARDING := :NEW.ONBOARDING;
    vID := :NEW.ID;
    vNAME := :NEW.NAME;
    vNETWORK_CODE := :NEW.NETWORK_CODE;

  ELSIF DELETING THEN
    vOperation := 'DELETE';
    vLOGON_TYPE := :OLD.LOGON_TYPE;
    vLAST_UPDATE := :OLD.LAST_UPDATE;
    vSTATUS := :OLD.STATUS;
    vADMIN_PASSWORD := :OLD.ADMIN_PASSWORD;
    vPASSWORD := :OLD.PASSWORD;
    vSESSION_VALID_SECONDS := :OLD.SESSION_VALID_SECONDS;
    vONBOARDING := :OLD.ONBOARDING;
    vID := :OLD.ID;
    vNAME := :OLD.NAME;
    vNETWORK_CODE := :OLD.NETWORK_CODE;

  END IF;

  INSERT INTO "MY_USER"."AUDIT" (TABLE_NAME, REGISTER_ID, OPERATION, DATETIME, PO_DOCUMENT)
  VALUES (
    vTableName,
    vID,
    vOperation,
    sysdate,
    json_object('logon_type' VALUE vLOGON_TYPE, 'last_update' VALUE vLAST_UPDATE, 'status' VALUE vSTATUS, 'admin_password' VALUE vADMIN_PASSWORD, 'password' VALUE vPASSWORD, 'session_valid_seconds' VALUE vSESSION_VALID_SECONDS, 'onboarding' VALUE vONBOARDING, 'id' VALUE vID, 'name' VALUE vNAME, 'network_code' VALUE vNETWORK_CODE)
  );

END;

执行此触发器并尝试插入后,我收到错误。但是,如果我删除此触发器并执行相同的插入,则没有问题。

  

[2017-10-10 10:38:08] [42000] [1722] ORA-01722:无效号码

     

[2017-10-10 10:38:08] ORA-06512:在“MY_USER.TRG_PARTNER”,第58行

     

[2017-10-10 10:38:08] ORA-04088:执行触发时出错   'MY_USER.TRG_PARTNER'

触发器有什么问题?这是我的插入内容:

INSERT INTO PARTNER
(ID, ADMIN_PASSWORD, LAST_UPDATE, LOGON_TYPE, NAME, ONBOARDING, PASSWORD, SESSION_VALID_SECONDS, STATUS, NETWORK_CODE)
VALUES ('example foo', 'foo', TO_DATE('2003/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'), 'T', 'foo', 'o', 'foo', 4344, 'A', 'foo');

第58行是 vNETWORK_CODE VARCHAR2(255 CHAR); 这不是数字

合作伙伴

CREATE TABLE PARTNER (
  ID                    VARCHAR2(255 CHAR) PRIMARY KEY,
  ADMIN_PASSWORD        VARCHAR2(255 CHAR) NOT NULL,
  LAST_UPDATE           DATE               NOT NULL,
  LOGON_TYPE            CHAR(1)            NOT NULL,
  NAME                  VARCHAR2(255 CHAR) NOT NULL,
  ONBOARDING            CHAR(1)            NOT NULL,
  PASSWORD              VARCHAR2(255 CHAR) NOT NULL,
  SESSION_VALID_SECONDS NUMBER(10, 0)      NOT NULL,
  STATUS                CHAR(1)            NOT NULL,
  NETWORK_CODE          VARCHAR2(255 CHAR),
  CONSTRAINT UK_PARTNER_NAME UNIQUE (NAME)
);

1 个答案:

答案 0 :(得分:0)

从评论中可以看出,AUDIT.REGISTER_ID具有数字数据类型,而vID具有字符类型。

因此INSERT将因您提到的错误而失败。

一种解决方案是将REGISTER_ID列的数据类型更改为VARCHAR2(255),以便它可以处理字符串。