Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2013
    Posts
    46

    Unanswered: better alternative to FOR LOOP

    Hi, I have a for loop to update a table and want to know if there is a better way to achieve this.

    FOR REC IN
    (SELECT DISTINCT E.EMP
    FROM EMPLOYEE E
    JOIN
    COMPANY C
    ON
    E.EMP=C.EMP
    WHERE C.FLAG='Y')
    LOOP
    UPDATE EMPLOYEE SET FLAG='Y' WHERE EMP=REC.EMP;
    END LOOP;

    Is there a better method to achieve the same result? Thx in advance.

  2. #2
    Join Date
    Aug 2003
    Location
    Where the Surf Meets the Turf @Del Mar, CA
    Posts
    7,776
    Provided Answers: 1
    NEVER do in PL/SQL that which can be done in plain SQL!

    Code:
    UPDATE employee 
    SET    flag = 'Y' 
    WHERE  emp IN (SELECT DISTINCT E.emp 
                   FROM   employee E 
                          join company C 
                            ON E.emp = C.emp 
                   WHERE  C.flag = 'Y')
    You can lead some folks to knowledge, but you can not make them think.
    The average person thinks he's above average!
    For most folks, they don't know, what they don't know.
    Good judgement comes from experience. Experience comes from bad judgement.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •