Search code examples
sqloraclefor-loopstored-procedurescursor

multiple loop inside single for statement using cursor


I have 2 cursors inside my procedure for which i want to use single for loop because using this i want to loop 2 variable inside single select query. The two variable has 2 different values which is used inside single select query. These 2 values are coming from KPI_DEFINITION table which gives me timestamp which i want to compare in my select query for data extraction. The first column KPI_FREQUENCY has value for example 30 and KPI_FREQ_TIME_UNIT column has value MINUTE. So if we combine these 2 column we will get 30 MINUTE and there is another combined column value which is 50 MINUTE and there might be more. So thats why i want to put this inside loop and compare in my select query with the start_date field value.But dont know how to do that. I simply use 2 cursors to take this 2 column and trying to loop it inside single for loop but dont know how to do that.There are might be another solution for this if i dont want to use two cursors but did not find a way.

create or replace PROCEDURE "EXT_TEST" AS 

LAST_WF_ID Number := 0;
LAST_UNIT NUMBER:=10;
LAST_UNIT_VAL VARCHAR2(20);

CURSOR KPI_FREQUENCY_CUR IS
Select KPI_FREQUENCY INTO LAST_UNIT from RATOR_MONITORING_CONFIGURATION.KPI_DEFINITION WHERE 
EVENT_ID = 10028;

CURSOR KPI_FREQ_TIME_UNIT_CUR IS
Select KPI_FREQ_TIME_UNIT INTO LAST_UNIT_VAL from RATOR_MONITORING_CONFIGURATION.KPI_DEFINITION WHERE 
EVENT_ID = 10028;

BEGIN

DBMS_OUTPUT.PUT_LINE('LAST_UNIT - ' || LAST_UNIT);
DBMS_OUTPUT.PUT_LINE('LAST_UNIT_VAL - ' || LAST_UNIT_VAL);

select MIN(ID) INTO LAST_WF_ID from WF_WORKFLOW@FONIC_RETAIL where start_date > sysdate - numtodsinterval(LAST_UNIT,LAST_UNIT_VAL);

DBMS_OUTPUT.PUT_LINE('LAST_WF_ID - ' || LAST_WF_ID);

END EXT_TEST;

Sample data from KPI_DEFINITION table:

KPI_DEF_ID  KPI_FREQUENCY KPI_FREQ_TIME_UNIT  EVENT_ID
1000136     30            MINUTE                10028   
1000137     50            MINUTE                10028

Solution

  • Pending seeing what data your actually on .. I'd suggest trying something a lot simpler ... such as this:

      select r.kpi_frequency, r.kpi_freq_time_unit, min(f.id) 
        from rator_monitoring_configuration.kpi_definition   r,
             wf_workflow@fonic_retail    f
       where r.event_id = 10028
         and f.start_date > sysdate - numtodsinterval ( r.kpi_frequency, r.kpi_freq_time_unit );
       group by r.kpi_frequency, r.kpi_freq_time_unit
       order by r.kpi_frequency, r.kpi_freq_time_unit;