触发器编译没有错误,但它不会添加数据

时间:2017-02-06 17:04:42

标签: sql oracle plsql database-trigger

我正在尝试更新名为' saturn.szrunsp'更新或插入后发生在桌面上#sat; szraspr'。在提出建议后,我创建了一个没有任何错误的触发器。然而,主要部分似乎仍然没有工作,这是在表saturn.szraspr上更新后将数据添加到表saturn.szrunsp的触发器。请查看下面的所有表格和触发器代码。更新!!

-- INSERTING table --

        CREATE TABLE SATURN.SZRUNSP
    (
      SZRUNSP_STUDENT_NO       VARCHAR2(9 CHAR)     NOT NULL,
      SZRUNSP_PIDM             NUMBER(8)            NOT NULL,
      SZRUNSP_STUDENT_NAME     VARCHAR2(60 CHAR)    NOT NULL,
      SZRUNSP_SUSPEND_ACCOUNT  VARCHAR2(1 CHAR)     NOT NULL,
      SZRUNSP_UNSUSPEND_DATE   DATE                 NOT NULL,
      SZRUNSP_ACTIVITY_DATE    DATE                 NOT NULL,
      SZRUNSP_USER_ID          VARCHAR2(30 CHAR),
      SZRUNSP_ACTION           VARCHAR2(30 CHAR),
      SZRUNSP_SURROGATE_ID     NUMBER(19)           NOT NULL,
      SZRUNSP_VERSION          NUMBER(19)           NOT NULL,
      SZRUNSP_VPDI_CODE        VARCHAR2(6 CHAR)
    )


-- TRIGGER TABLE -- 

    CREATE TABLE SATURN.SZRASPR
(
  SZRASPR_STUDENT_NO             VARCHAR2(9 CHAR) NOT NULL,
  SZRASPR_STUDENT_NAME           VARCHAR2(60 CHAR) NOT NULL,
  SZRASPR_DATE_OF_BIRTH          VARCHAR2(8 CHAR) NOT NULL,
  SZRASPR_SUSPEND_ACCOUNT        VARCHAR2(1 CHAR) NOT NULL,
  SZRASPR_EMAIL_ADDRESS          VARCHAR2(100 CHAR) NOT NULL,
  SZRASPR_STUDENT_YEAR           VARCHAR2(2 CHAR) NOT NULL,
  SZRASPR_FACULTY                VARCHAR2(60 CHAR) NOT NULL,
  SZRASPR_SCHOOL_OF_STUDY        VARCHAR2(60 CHAR) NOT NULL,
  SZRASPR_DEGREE_NAME            VARCHAR2(100 CHAR) NOT NULL,
  SZRASPR_MODE_OF_STUDY          VARCHAR2(20 CHAR) NOT NULL,
  SZRASPR_FEE_STATUS             VARCHAR2(20 CHAR) NOT NULL,
  SZRASPR_YEAR_START             VARCHAR2(4 CHAR) NOT NULL,
  SZRASPR_SEMESTER_START         VARCHAR2(5 CHAR) NOT NULL,
  SZRASPR_ENROLMENT_STATUS       VARCHAR2(2 CHAR) NOT NULL,
  SZRASPR_LEAVING_DATE           VARCHAR2(8 CHAR),
  SZRASPR_PENDING_ACTIVITY_DATE  DATE,
  SZRASPR_ACTIVE_ACTIVITY_DATE   DATE,
  SZRASPR_PENDING_STATUS         VARCHAR2(2 CHAR),
  SZRASPR_ACTIVE_STATUS          VARCHAR2(2 CHAR),
  SZRASPR_PIDM                   NUMBER(8)      NOT NULL,
  SZRASPR_TERM_CODE              VARCHAR2(6 CHAR) NOT NULL,
  SZRASPR_ASPIRE_ERROR_REPORTED  VARCHAR2(10 CHAR),
  SZRASPR_ERROR_TEXT             VARCHAR2(100 CHAR),
  SZRASPR_SEQ_NO                 NUMBER,
  SZRASPR_SEND                   INTEGER,
  SZRASPR_SURROGATE_ID           NUMBER(19),
  SZRASPR_VERSION                NUMBER(19),
  SZRASPR_USER_ID                VARCHAR2(30 CHAR),
  SZRASPR_DATA_ORIGIN            VARCHAR2(30 CHAR),
  SZRASPR_ACTIVITY_DATE          DATE,
  SZRASPR_VPDI_CODE              VARCHAR2(6 CHAR)
);



--Trigger--

    CREATE OR REPLACE TRIGGER szrunsp_suspend_trigger
after update or insert on SATURN.SZRASPR
for each row     
declare
    SZRUNSP_USER_ID varchar2(30 CHAR);
begin
    select user into SZRUNSP_USER_ID from dual;

    insert into SATURN.SZRUNSP values
        (:new.SZRASPR_STUDENT_NO,
         :new.SZRASPR_PIDM,
         :new.SZRASPR_STUDENT_NAME,
         :new.SZRASPR_SUSPEND_ACCOUNT,
         SYSDATE,
         SYSDATE,
         user,
         :new.SZRASPR_SURROGATE_ID,
         :new.SZRASPR_VERSION,
         :new.SZRASPR_VPDI_CODE);
 end;
/

2 个答案:

答案 0 :(得分:1)

您正在将值插入

  SZRUNSP_SURROGATE_ID     NUMBER(19)           NOT NULL,
  SZRUNSP_VERSION          NUMBER(19)           NOT NULL,

你只能在触发表的列上使用:new前缀 - 而不是在你插入的表中

答案 1 :(得分:1)

不确定为什么会收到此错误:

[Error] ORA-00947 (9: 20): PL/SQL: ORA-00947: not enough values

INSERT语句中的值数与您发布的表匹配。因此,如果您收到该错误,则需要更正您的问题。

[Error] PLS-00049 (10: 2): PLS-00049: bad bind variable 'NEW.SZRUNSP_STUDENT_NO'

INSERT语句中引用的所有列都与目标表saturn.szrunsp具有相同的名称。但是,绑定名称必须与触发表的列匹配。 saturn.szraspr。你说

  

saturn.szraspr有szraspr_student_no列

因此您需要编辑匹配的触发器:

CREATE OR REPLACE TRIGGER szrunsp_suspend_trigger
after update or insert on szraspr
for each row     
declare
    szrunsp_user_id varchar2(30);
begin
    select user into szrunsp_user_id from dual;

    insert into szrunsp values
        (:new.SZRASPR_STUDENT_NO,
         :new.SZRASPR_PIDM,
         :new.SZRASPR_STUDENT_NAME,
         :new.SZRASPR_SUSPEND_ACCOUNT,
         SYSDATE,
         SYSDATE,
         user,
         :new.SZRASPR_ACTION,
         '',
         '',
         '');
 end;
/

在Oracle中,空字符串为null,因此您需要处理SZRUNSP_SURROGATE_ID和SZRUNSP_VERSION上的NOT NULL违规。