1

Please, help me to convert this loop statement:

BEGIN
   FOR employee_rec in (SELECT * FROM BONUS)
   LOOP
      IF employee_rec.BONVALUE > 500 THEN
        UPDATE BONUS
        SET BONUS.TAX = BONUS.BONVALUE * 0.12 WHERE employee_rec.BONVALUE = BONUS.BONVALUE;
      ELSIF 
      -- ...
      END IF;

   END LOOP;
END;

into statement where CURSOR being used.

4
  • Generally we would like to see what sort of attempt you have made at solving the problem yourself - as well as where you are hitting problems. Requests for code generally don't go very well. Commented Dec 21, 2015 at 15:48
  • I didn't make attempt bc I don't fully understand syntax and usage of cursor. Hope example of conversion will clarify for me both aspects. Commented Dec 21, 2015 at 15:51
  • 2
    You're already using a cursor. That's what the SELECT * FROM BONUS is. If you want to name that cursor, well, it's not that difficult... simply define a cursor in the declaration section and give it a name. However, it looks like in this instance, you don't need a cursor-for-loop at all; I'm guessing that the other IF clauses are also updates to the bonus table? If so, you ought to be able to achieve this in a single update statement Commented Dec 21, 2015 at 15:52
  • As an aside, I've met a lot of Oracle developers in my time who (wrongly) don't refer to an implicit cursor as a cursor. Just an observation. Commented Dec 21, 2015 at 16:05

1 Answer 1

3

What you have is a loop based on an implicit cursor definition. It sounds like you want a loop based on an explicit cursor definition.

As mentioned in the comments. Just define your cursor explicitly in the declaration section.

DECLARE
   CURSOR cur_bonus IS
   SELECT *
   FROM bonus;
BEGIN
   FOR employee_rec IN cur_bonus
   LOOP
      IF employee_rec.BONVALUE > 500 THEN
        UPDATE BONUS
        SET BONUS.TAX = BONUS.BONVALUE * 0.12 WHERE employee_rec.BONVALUE = BONUS.BONVALUE;
      ELSIF 
      -- ...
      END IF;

   END LOOP;
END;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.