我的触发器接受输入。(新:input_id)使用此输入,它将使用动态生成的查询生成新的 ID。动态生成查询的指令存储在 QUERY_REF 中。
它使用游标检索 query_ids 以生成动态查询:
CURSOR C_QUERY IS
SELECT QUERY_ID
FROM QUERY_REF
WHERE GENERATE_IND='Y';
OPEN C_QUERY;
LOOP
FETCH C_QUERY INTO QUERY_ID_RET;
EXIT WHEN C_QUERY%NOTFOUND;
INPUT_ID 和 QUERY_ID_RET 已成功用于在函数 gethostID 中创建 Output_ID。
我正在尝试根据是否找到记录来运行插入、更新。选择计数返回 0。我发现选择计数返回 0 的原因——我引用了错误的表。我仍然不知道为什么这是检索旧数据。这是整个触发器:
create or replace
TRIGGER INPUT_AUTO_QUERY_TRIG
AFTER INSERT OR UPDATE
ON INPUT_TABLE
FOR EACH ROW
DECLARE
ACTION_VALUE VARCHAR2(6);
HOLD_EVENT_ID VARCHAR2(256);
HOLD_USER_ID VARCHAR2(30);
HOLD_PK_VALUE INTEGER(10);
HOLD_AUDIT_ITEM_ID INTEGER(10);
QUERY_ID_RET NUMBER;
resultcount NUMBER;
HostID VARCHAR2(256);
QUERY_ID NUMBER;
INPUT_ID NUMBER(38,0);
GENERATE_IND VARCHAR2(1);
pragma autonomous_transaction;
CURSOR C_QUERY IS
SELECT QUERY_ID
FROM QUERY_REF
WHERE GENERATE_IND='Y';
BEGIN
OPEN C_QUERY;
LOOP
FETCH C_QUERY INTO QUERY_ID_RET;
EXIT WHEN C_QUERY%NOTFOUND;
SELECT AUDIT_ID, USER_ID
INTO HOLD_EVENT_ID, HOLD_USER_ID
FROM AUDIT_EVENT_TEMP
WHERE SESSION_ID = SYS_CONTEXT('USERENV', 'SESSIONID');
OutputID:=getHostID(:NEW.INPUT_ID,QUERY_ID_RET);
IF INSERTING THEN
INSERT INTO DETAIL
(
DETAIL_ID,
INPUT_ID,
OUTPUT_ID,
QUERY_ID,
ACTIVE_IND,
CREATED_BY,
DATE_CREATED,
MODIFIED_BY,
DATE_MODIFIED
) VALUES
(
DETAIL_SEQ.NEXTVAL,
:NEW.INPUT_ID,
OutputID,
QUERY_ID_RET,
'Y',
HOLD_USER_ID,
SYSDATE,
HOLD_USER_ID,
SYSDATE
);
ELSIF UPDATING THEN
SELECT COUNT(QUERY_ID) INTO resultcount FROM DETAIL WHERE INPUT_ID=:NEW.INPUT_ID AND QUERY_ID=QUERY_ID_RET;
IF resultcount>0 THEN
UPDATE PATIENT_DATA_SOURCE
SET
HOST_ID = HostID,
ACTIVE_IND ='Y',
MODIFIED_BY =HOLD_USER_ID,
DATE_MODIFIED =SYSDATE
WHERE INPUT_ID=:NEW.INPUT_ID
AND QUERY_ID=QUERY_ID_RET;
--don't want to change: DETAIL_ID,QUERY_ID,INPUT_ID,CREATED_BY, DATE_CREATED in update
ELSE
INSERT INTO DETAIL
(
DETAIL_ID,
INPUT_ID,
HOST_ID,
QUERY_ID,
ACTIVE_IND,
CREATED_BY,
DATE_CREATED,
MODIFIED_BY,
DATE_MODIFIED
) VALUES
(
DETAIL_SEQ.NEXTVAL,
:NEW.INPUT_ID,
HostID,
QUERY_ID_RET,
'Y',
HOLD_USER_ID,
SYSDATE,
HOLD_USER_ID,
SYSDATE
);
END IF;
--end if insert or update inside update
END IF;
--end IF UPDATING
END LOOP;
Close C_QUERY;
COMMIT;
END INPUT_AUTO_QUERY_TRIG;
--end trigger