Search code examples
sqloracleplsqlcursor

Why using cursors in PL/SQl ORACLE?


I'm a beginner at PL/SQL, and during studying the course I saw CURSOR and I want to know why we should use it, and when? thank you very much


Solution

  • When you do a SELECT and it returns more than one row you can't save the rows in a variable, so you'll have to use a CURSOR. If you are familiar with programming a CURSOR is something like an Array. So if you do a SELECT and save the results in a variable like in the code:

    SELECT id INTO v_id FROM table;
    

    and if more than one row is returned, you cant save the rows in the variable v_id, and a TOO_MANY_ROWS Exception will be thrown. Reference: http://www.oracle.com/technetwork/issue-archive/2013/13-mar/o23plsql-1906474.html