Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8181733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:37:19+00:00 2026-06-07T00:37:19+00:00

My trigger takes an input. (NEW:input_id) With this input, it will generate new ID’s

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:37:21+00:00Added an answer on June 7, 2026 at 12:37 am

    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)?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The trigger below is delaying my insert response. How can I prevent this? create
I have a trigger that looks something like this: create or replace TRIGGER cluster_check
I am using CONTEXT_INFO to pass a username to a delete trigger for the
Its about a small android application that will take hours as input from user
I have one question regarding trigger. The scenario is like this Create Procedure begin
This query appears in mysql slow query log: it takes 11 seconds. INSERT INTO
I have a service that will run every X minutes. If that job takes
when I update 100 records from the table with the trigger enabled it takes
I want to create a trigger on a table called takes in postgresql to
I just wrote a small program which takes the input from user and shows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.