11 Replies Latest reply on Apr 12, 2019 5:52 AM by Billy~Verreynne

    What's the real use of Cursors?

    Mark1991

      Hi everyone and thank you for taking your time reading this, probably, silly question of mine.

      I've been recently tried to figure out the correct use of these so called Cursors and it's not so complicated but what I can't truly figure out is the practical use for them. I'll provide an example down below to better clarify my question:

      declare
          -- Declaring a Variable:
          v_name varchar2(30);
          -- Declaring a Cursor:
          cursor cur_name is
          select first_name
          from employees
          where employee_id < 150;
      begin
          open cur_name;
          loop
              fetch cur_name into v_name;
              dbms_output.put_line (v_name);
      -- In order to avoid an infinite cycle I use the function below:
              exit when cur_name%notfound;
          end loop;
          close cur_name;
      end;
      

      Now, from this Query I am gonna get a bunch of names, let's say Mark, Alex, Rick, Doug. But if I were to simply type the following Query:

      select first_name
          from employees
          where employee_id < 150;
      

      I'd get the exact same result. So what's the use of Cursors? Why should I bother typing such a "huge" Query instead of a simple SQL Select Statement? Maybe this example is too reductive and it won't let me see the big picture but I truly don't get it. Thanks in advance