+ Reply to Thread + Post New Thread
Results 1 to 4 of 4
  1. #1
    laxman's Avatar
    laxman is offline Forum Expert
    Join Date
    21 Mar 2009
    Location
    MUMBAI
    Posts
    138
    Say Thanks
    6
    Thanked 0 Times in 0 Posts
    Documents
    0
    Uploads
    0

    Default multiple cursor with date handling issue reg

    Dear all,
    i am using three cursors in one pl/sql block each of this three cursor will fetch data based on column of date data type.i believe due to each condition relate to same table,contradiction may arise i.e

    1> when code will execute , cursor c1 will do the job perfectly but c2 & c3 here may not be able to solve the purpose since the job has already been done by cursor c1 which i am not sure.my doubts i need all cursor to work based on each condition.
    any suggestion from your side could sort out this issues.

    Thanks n regards
    Laxman



    Code sql:
    declare

    begin

    FOR c1 IN(SELECT reqid FROM request WHERE lastmoddate<sysdate - 8/24 AND statuscode=1 AND assigned_personid IS NOT NULL)loop
                         
                         UPDATE srequest SET STATUS=open WHERE reqid=c1.reqid;
                         commit;
             
              end loop;

    FOR c2 IN(SELECT reqid FROM request WHERE lastmoddate<sysdate - 1 AND statuscode=1 AND assigned_personid IS NULL)loop
                         
                         UPDATE srequest SET STATUS=open WHERE reqid=c2.reqid;
                         commit;
             
              end loop;

    FOR c3 IN(SELECT reqid FROM request WHERE lastmoddate<sysdate - 14 AND statuscode=1 AND assigned_personid IS NULL)loop
                         
                         UPDATE srequest SET STATUS=open WHERE reqid=c3.reqid;
                         commit;
             
              end loop;

    end;

  2.    Club-Oracle Complementary E-Books and Magazines
    Get your free Complementary Copy of Oracle Magazine

    You can also browse the Free Magazines and E-Books section to see the complete list of free magazines, e-books and Whitepapers.

  3. #2
    zargon's Avatar
    zargon is offline Forum Genius
    Join Date
    22 Oct 2008
    Location
    Here, or maybe there, or ...
    Posts
    473
    Say Thanks
    0
    Thanked 38 Times in 37 Posts
    Documents
    0
    Uploads
    0

    Default Re: multiple cursor with date handling issue reg

    Quote Originally Posted by laxman View Post
    Dear all,
    i am using three cursors in one pl/sql block each of this three cursor will fetch data based on column of date data type.i believe due to each condition relate to same table,contradiction may arise i.e

    1> when code will execute , cursor c1 will do the job perfectly but c2 & c3 here may not be able to solve the purpose since the job has already been done by cursor c1 which i am not sure.my doubts i need all cursor to work based on each condition.
    any suggestion from your side could sort out this issues.

    Thanks n regards
    Laxman



    Code sql:
    declare

    begin

    FOR c1 IN(SELECT reqid FROM request WHERE lastmoddate<sysdate - 8/24 AND statuscode=1 AND assigned_personid IS NOT NULL)loop
                         
                         UPDATE srequest SET STATUS=open WHERE reqid=c1.reqid;
                         commit;
             
              end loop;

    FOR c2 IN(SELECT reqid FROM request WHERE lastmoddate<sysdate - 1 AND statuscode=1 AND assigned_personid IS NULL)loop
                         
                         UPDATE srequest SET STATUS=open WHERE reqid=c2.reqid;
                         commit;
             
              end loop;

    FOR c3 IN(SELECT reqid FROM request WHERE lastmoddate<sysdate - 14 AND statuscode=1 AND assigned_personid IS NULL)loop
                         
                         UPDATE srequest SET STATUS=open WHERE reqid=c3.reqid;
                         commit;
             
              end loop;

    end;
    The real problem with this code is the commits inside loops. Rewrite the code like this:

    Code sql:
    begin

    FOR c1 IN(SELECT reqid FROM request WHERE lastmoddate<sysdate - 8/24 AND statuscode=1 AND assigned_personid IS NOT NULL)loop
                         
                         UPDATE srequest SET STATUS=open WHERE reqid=c1.reqid;
             
              end loop;

    commit;

    FOR c2 IN(SELECT reqid FROM request WHERE lastmoddate<sysdate - 1 AND statuscode=1 AND assigned_personid IS NULL)loop
                         
                         UPDATE srequest SET STATUS=open WHERE reqid=c2.reqid;
             
              end loop;

    commit;

    FOR c3 IN(SELECT reqid FROM request WHERE lastmoddate<sysdate - 14 AND statuscode=1 AND assigned_personid IS NULL)loop
                         
                         UPDATE srequest SET STATUS=open WHERE reqid=c3.reqid;
             
              end loop;

    commit;

    end;
    /

    As for your concern that the cursors 'may not be able to solve the purpose' I wonder why you think such things. I suggest you run the queries in sequence and note that they do, indeed, 'solve the purpose'.

  4. #3
    laxman's Avatar
    laxman is offline Forum Expert
    Join Date
    21 Mar 2009
    Location
    MUMBAI
    Posts
    138
    Say Thanks
    6
    Thanked 0 Times in 0 Posts
    Documents
    0
    Uploads
    0

    Default Re: multiple cursor with date handling issue reg

    Thanks sir for ur kind responce,yes i agree this will definitly work,what my intention was that all the three cursor are doing the same job based on different condition.if u can see c2 and c3 .

    cursor c2---> lastmoddate< sysdate - 1

    cursor c3 ---> lastmoddate< sysdate - 14

    job of cursor c3 is already been done by c2 since sysdate - 14 < sysdate - 1,this may cause a replicate of data when c3 will start execute.what i need here is in order to avoid the replicate of data can i merge all the job into single cursor ??? will it work !!!

    need ur suggestion kindly

    Regards
    Laxman

  5. #4
    rajavu's Avatar
    rajavu is offline Forum Genius
    Join Date
    13 Oct 2008
    Location
    @ Bangalore , India
    Posts
    471
    Say Thanks
    0
    Thanked 20 Times in 19 Posts
    Documents
    0
    Uploads
    0

    Default Re: multiple cursor with date handling issue reg

    It could be merged as


    begin

    Code sql:
    FOR c IN(SELECT reqid
              FROM request
             WHERE statuscode=1
               AND (( assigned_personid IS NULL  AND ( sysdate - 1 OR lastmoddate<sysdate - 14  ))
                    OR
                    ( assigned_personid IS NOT NULL  AND ( lastmoddate<sysdate - 8/24 )))
             )
                   loop
                         
                         UPDATE srequest SET STATUS='open' WHERE reqid=c.reqid;
             
              end loop;

    commit;

    Or using straight SQL as follows


    Code sql:
    UPDATE  request
       SET  STATUS='Open'
     WHERE statuscode=1
       AND (( assigned_personid IS NULL  AND ( sysdate - 1 OR lastmoddate<sysdate - 14  ))
            OR
            ( assigned_personid IS NOT NULL  AND ( lastmoddate<sysdate - 8/24 )));

    Raj.

Similar Threads

  1. cursor with exception issue reg
    By laxman in forum SQL PL/SQL
    Replies: 6
    Last Post: 03-10-2010, 06:49 AM
  2. date manipultation issue reg
    By laxman in forum SQL PL/SQL
    Replies: 2
    Last Post: 03-04-2010, 03:02 AM
  3. Oracle SQL Nested Cursor Issue
    By laxman in forum SQL PL/SQL
    Replies: 3
    Last Post: 02-25-2010, 07:08 PM
  4. nested cursor loop issue reg.
    By laxman in forum SQL PL/SQL
    Replies: 2
    Last Post: 02-24-2010, 08:12 AM

Tags for this Thread