Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

What's the real use of Cursors?

Mark1991Apr 10 2019 — edited Apr 12 2019

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

This post has been answered by GregV on Apr 10 2019
Jump to Answer
Comments
Post Details
Added on Apr 10 2019
11 comments
985 views