我创建了用于获取主表事务的日志表,但是当我插入主表时,日志表中的ID为NULL

时间:2018-06-21 09:19:04

标签: c# oracle

首先,我创建了Main表,之后,为了进行一条记录的插入,更新,删除事务,我创建了Log表。请注意,我在Main表中使用了自动递增。

主表的表结构

1..Table Structure of Main Table

主表数据

2..Data of Main Table

日志表的表结构

3..Table Structure of Log Table

日志表的数据-交易

4..Data of Log Table - Transaction

表定义:

CREATE TABLE departments 
(
    ID           NUMBER(10)    NOT NULL,
    DESCRIPTION  VARCHAR2(50)  NOT NULL
);

ALTER TABLE departments ADD (
  CONSTRAINT dept_pk PRIMARY KEY (ID));

CREATE SEQUENCE dept_seq START WITH 1;

触发器定义:

CREATE OR REPLACE TRIGGER dept_bir 
BEFORE INSERT ON departments 
FOR EACH ROW

BEGIN
  SELECT dept_seq.NEXTVAL
  INTO   :new.id
  FROM   dual;
END;`enter code here
/

我的问题是:如果我插入一行,但是在日志表中,id的值为NULL。

我在Oracle中的存储过程,用于插入和更新MainLog表:

CREATE OR REPLACE PROCEDURE P_SAVE_YARDMASTER_DATA(
    IP_YARD_NAME     IN VARCHAR2,
    IP_MATERIAL_TYPE IN VARCHAR2,
    IP_MATERIAL_NAME IN VARCHAR2,
    IP_CAPACITY      IN VARCHAR2,
    -- IP_RID OUT NUMBER ,
    IP_IS_DELETE    IN NUMBER ,
    IP_LAST_UPD_UID IN VARCHAR2,
    IP_OPERATION    IN VARCHAR2,
    updaterowid     IN VARCHAR2 := NULL,
    OUT_RETURN_MSG OUT VARCHAR2,
    OUT_RETURN_CODE OUT NUMBER)
IS
BEGIN
  OUT_RETURN_MSG  := '';
  OUT_RETURN_CODE := 0;
  IF IP_OPERATION  = 'INSERT' THEN
    INSERT
    INTO MST_VP_YARD
      (
        YARD_NAME,
        MATERIAL_TYPE,
        MATERIAL_NAME,
        CAPACITY,
        IS_DELETE,
        LAST_UPD_IS,
        LAST_UPD_UID
      )
      VALUES
      (
        IP_YARD_NAME ,
        IP_MATERIAL_TYPE ,
        IP_MATERIAL_NAME ,
        IP_CAPACITY ,
        IP_IS_DELETE ,
        SYSDATE ,
        IP_LAST_UPD_UID
      );
              INSERT
        INTO MST_VP_YARD_LOG
          (
            YARD_NAME,
            MATERIAL_TYPE,
            MATERIAL_NAME,
            CAPACITY,
           IS_DELETE,
            LAST_UPD_IS,
            LAST_UPD_UID

          )
          VALUES
          (

            IP_YARD_NAME ,
            IP_MATERIAL_TYPE ,
            IP_MATERIAL_NAME ,
            IP_CAPACITY ,
            IP_IS_DELETE ,
            SYSDATE ,
            IP_LAST_UPD_UID

          );
  END IF;
  IF IP_OPERATION = 'UPDATE' THEN
    UPDATE MST_VP_YARD
    SET YARD_NAME   = IP_YARD_NAME,
      MATERIAL_TYPE = IP_MATERIAL_TYPE,
      MATERIAL_NAME = IP_MATERIAL_NAME,
      CAPACITY      = IP_CAPACITY,
      IS_DELETE     = IP_IS_DELETE,
      LAST_UPD_IS   = SYSDATE,
      LAST_UPD_UID  = IP_LAST_UPD_UID
    WHERE RID       = updaterowid;
    INSERT
    INTO MST_VP_YARD_LOG
      (
        RID,
        YARD_NAME,
        MATERIAL_TYPE,
        MATERIAL_NAME,
        CAPACITY,
        IS_DELETE,
        LAST_UPD_IS,
        LAST_UPD_UID
      )
      VALUES
      (
        updaterowid,
        IP_YARD_NAME ,
        IP_MATERIAL_TYPE ,
        IP_MATERIAL_NAME ,
        IP_CAPACITY ,
        IP_IS_DELETE ,
        SYSDATE ,
        IP_LAST_UPD_UID
      );
  END IF;
  OUT_RETURN_MSG  := 'SUCCESS';
  OUT_RETURN_CODE := 1;
EXCEPTION
WHEN OTHERS THEN
  OUT_RETURN_MSG  := SQLERRM;
  OUT_RETURN_CODE := 0;
END P_SAVE_YARDMASTER_DATA;

2 个答案:

答案 0 :(得分:0)

您正在使用触发器生成ID。因此,您可以使用RETURNING INTO语句将生成的ID提取到变量中。现在使用该ID插入日志表。 检查一下

Oracle DML Returning Into Clause

答案 1 :(得分:0)

您可以使用id syntax获得Departments表的returning。 然后在插入log表中时使用该ID。

类似这样的东西:

CREATE OR REPLACE PROCEDURE P_SAVE_YARDMASTER_DATA(
    IP_YARD_NAME     IN VARCHAR2,
    IP_MATERIAL_TYPE IN VARCHAR2,
    IP_MATERIAL_NAME IN VARCHAR2,
    IP_CAPACITY      IN VARCHAR2,
    -- IP_RID OUT NUMBER ,
    IP_IS_DELETE    IN NUMBER ,
    IP_LAST_UPD_UID IN VARCHAR2,
    IP_OPERATION    IN VARCHAR2,
    updaterowid     IN VARCHAR2 := NULL,
    OUT_RETURN_MSG OUT VARCHAR2,
    OUT_RETURN_CODE OUT NUMBER)
IS
    my_id number;
BEGIN
  OUT_RETURN_MSG  := '';
  OUT_RETURN_CODE := 0;
  IF IP_OPERATION  = 'INSERT' THEN
    INSERT
    INTO MST_VP_YARD
      (
        YARD_NAME,
        MATERIAL_TYPE,
        MATERIAL_NAME,
        CAPACITY,
        IS_DELETE,
        LAST_UPD_IS,
        LAST_UPD_UID
      )
      VALUES
      (
        IP_YARD_NAME ,
        IP_MATERIAL_TYPE ,
        IP_MATERIAL_NAME ,
        IP_CAPACITY ,
        IP_IS_DELETE ,
        SYSDATE ,
        IP_LAST_UPD_UID
      ) RETURNING id INTO my_id;
              INSERT
        INTO MST_VP_YARD_LOG
          (
            RID,  
            YARD_NAME,
            MATERIAL_TYPE,
            MATERIAL_NAME,
            CAPACITY,
            IS_DELETE,
            LAST_UPD_IS,
            LAST_UPD_UID

          )
          VALUES
          (
            my_id,
            IP_YARD_NAME ,
            IP_MATERIAL_TYPE ,
            IP_MATERIAL_NAME ,
            IP_CAPACITY ,
            IP_IS_DELETE ,
            SYSDATE ,
            IP_LAST_UPD_UID

          );
  END IF;
  IF IP_OPERATION = 'UPDATE' THEN
    UPDATE MST_VP_YARD
    SET YARD_NAME   = IP_YARD_NAME,
      MATERIAL_TYPE = IP_MATERIAL_TYPE,
      MATERIAL_NAME = IP_MATERIAL_NAME,
      CAPACITY      = IP_CAPACITY,
      IS_DELETE     = IP_IS_DELETE,
      LAST_UPD_IS   = SYSDATE,
      LAST_UPD_UID  = IP_LAST_UPD_UID
    WHERE RID       = updaterowid;
    INSERT
    INTO MST_VP_YARD_LOG
      (
        RID,
        YARD_NAME,
        MATERIAL_TYPE,
        MATERIAL_NAME,
        CAPACITY,
        IS_DELETE,
        LAST_UPD_IS,
        LAST_UPD_UID
      )
      VALUES
      (
        updaterowid,
        IP_YARD_NAME ,
        IP_MATERIAL_TYPE ,
        IP_MATERIAL_NAME ,
        IP_CAPACITY ,
        IP_IS_DELETE ,
        SYSDATE ,
        IP_LAST_UPD_UID
      );
  END IF;
  OUT_RETURN_MSG  := 'SUCCESS';
  OUT_RETURN_CODE := 1;
EXCEPTION
WHEN OTHERS THEN
  OUT_RETURN_MSG  := SQLERRM;
  OUT_RETURN_CODE := 0;
END P_SAVE_YARDMASTER_DATA;