Reputation: 505
My trigger takes an input. (NEW:input_id) With this input, it will generate new ID's using a dynamically generate queries. The instructions
for the dynamically generated queries are stored in QUERY_REF.
It uses a cursor to retrieve query_ids for generating dynamic queries:
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 and QUERY_ID_RET have been used succesfully to create Output_ID in function gethostID.
I'm trying to run an insert, update based on whether a record is found. The Select counts are returning 0. I discovered why the select counts were returning 0--I referenced the wrong table. I still don't know why this is retrieving old dat. Here is the entire trigger:
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
Upvotes: 1
Views: 8367
Reputation: 4148
There are no restrictions on using SELECT COUNT(*) in a trigger - unless it is on the table the trigger is defined on (possibility of a mutating table).
http://psoug.org/reference/table_trigger.html
Does SELECT COUNT(*) FROM query_str_ref WHERE INPUT_ID=:NEW.INPUT_ID AND QUERY_ID=QUERY_ID_RET;
work on its own in an SQLPLUS session and return a count more than zero?
If this line returns the correct count outside the trigger, then either something in the trigger could be impacting the count before the line is reached or the line might not be reached at all.
If this line DOESN'T return the correct count outside the trigger then it might be a good idea to check the QUERY_STR_REF table data, the new value input ID and the
Also, are you sure that the CURSOR /FETCH
on QUERY_REF to set QUERY_ID_RET is either inside
the trigger or accessible from the trigger,
and there is no interference (QUERY_REF is
not being altered by the triggering statement)?
Upvotes: 1