首先,我创建了Main
表,之后,为了进行一条记录的插入,更新,删除事务,我创建了Log
表。请注意,我在Main
表中使用了自动递增。
主表的表结构
主表数据
日志表的表结构
日志表的数据-交易
表定义:
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中的存储过程,用于插入和更新Main
和Log
表:
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;
答案 0 :(得分:0)
您正在使用触发器生成ID。因此,您可以使用RETURNING INTO
语句将生成的ID提取到变量中。现在使用该ID插入日志表。
检查一下
答案 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;