-1

I have two cursors as below:

EXEC SQL                                      
     DECLARE C1 CURSOR FOR               
     SELECT  DISTINCT FIELD_1                  
     FROM    TABLE                  
     WHERE   FIELD_2 BETWEEN :MAX_DATE
                        AND '9999-12-31'      
           AND   FIELD_3 =  :VALUE 
     WITH UR                                  
     FOR FETCH ONLY                           
END-EXEC

EXEC SQL                                      
     DECLARE C2 CURSOR FOR               
     SELECT  DISTINCT FIELD_1                  
     FROM    TABLE                  
     WHERE   FIELD_2 BETWEEN :MIN_DATE
                        AND '9999-12-31'      
           AND   FIELD_3 =  :VALUE 
     WITH UR                                  
     FOR FETCH ONLY                           
END-EXEC 

Both the cursors are pointing to same table. How can I combine the two cursors? Please let me know a way to combine the two tables.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sunitha S
  • 1
  • 1
  • 3

1 Answers1

0

If you just want to combine the predicates of both cursors, of course you could do. Combine the WHERE clauses:

EXEC SQL                                      
     DECLARE C1 CURSOR FOR               
     SELECT  DISTINCT FIELD_1                  
     FROM    TABLE                  
     WHERE   FIELD_2 BETWEEN :MIN_DATE
                        AND :MAX_DATE
           AND   FIELD_3 =  :VALUE 
     WITH UR                                  
     FOR FETCH ONLY                           
END-EXEC

Your example in the question had both min and max values in a range with 9999-12-31 which doesn't make sense to me.

data_henrik
  • 16,724
  • 2
  • 28
  • 49
  • 'DISTINCT' is taking too long time for execution. Can we use anything else instead of 'DISTINCT' so that we can save CPU time and Cost. – Sunitha S Aug 09 '17 at 07:31
  • i have a cursor like this with 'DISTINCT' clause. This cursor is taking more time to execute in Open Cursor statement. please tell me a way to optimize. Can i use 'ORDER BY' or 'GROUP BY' here – Sunitha S Aug 09 '17 at 08:37
  • EXEC SQL DECLARE CSR CURSOR FOR SELECT DISTINCT A.FIELD1 ,C.FIELD2 FROM TABLE1 A ,TABLE2 B ,TABLE3 C WHERE A.FIELD3= :VALUE.... – Sunitha S Aug 09 '17 at 08:39