Cursors: You can define Cursor Statements in Declaration section. Cursor statements let you bring data from tables and views into your PL/SQL Programs.
Why use cursors? Well when you retrieve subset of data from table (or whole table), then that data remains stored in SGA (Shared memory) until cursor is closed, so in this way you cache data and caching on database is good idea.
Choosing explicit or implicit cursor in your PL/SQL program?
Implicit cursors are used when you have a simple SELECT ... INTO single row of data into local program variables. It's the easiest path to your data, but it can often lead to coding the same or similar SELECTs in multiple places in your code.
Explicit cursors are defined in declaration section (package or block) and in this way, you can open and fetch from cursor in one or more places.
Implicit cursor will run more efficient than equivalent explicit cursor (from Oracle 8 Database onwards). So is there reasons to use explicit cursors at all? Off course. Explicit cursor can still be more efficient and they off course offer much programmatic control.
Implicit cursor
Implicit cursors are used when you need to retrieve single row from database. If you want to retrieve more than one row, then you must use either an explicit cursor or bulk collect.
Here one example of implicit cursor usage:
We encapsulate query with function (this is _aways_ a good idea). This function print employee information from database to output. Also we introduce some exception handling (when
no employee is found).
Because PL/SQL is so tightly integrated with the Oracle database, you can easily retrieve complex datatypes (entire row for example - as we did in our example).
You can see that using implicit cursor is quite simple (with basic understanding of SQL) we just create simple select statement and insert rowset into record (that we declared as local variable).
Explicit cursor
Explicit cursor is explicitly defined in the declaration section. With explicit cursor, you have complete control over the different PL/SQL steps involved in retrieving information from the database. You decide when to open, when fetch and how many records and when to close cursor. Information about the current state of cursor is available through examination of cursor attributes.
Example:
This PL/SQL block performs following:
Declare the cursor.
Declare а record based on that cursor.
Open the cursor.
Fetch rows until there are no rows left.
Close cursor.
Handle exception and close cursor if it is not closed.
Copied from link
Why use cursors? Well when you retrieve subset of data from table (or whole table), then that data remains stored in SGA (Shared memory) until cursor is closed, so in this way you cache data and caching on database is good idea.
Choosing explicit or implicit cursor in your PL/SQL program?
Implicit cursors are used when you have a simple SELECT ... INTO single row of data into local program variables. It's the easiest path to your data, but it can often lead to coding the same or similar SELECTs in multiple places in your code.
Explicit cursors are defined in declaration section (package or block) and in this way, you can open and fetch from cursor in one or more places.
Implicit cursor will run more efficient than equivalent explicit cursor (from Oracle 8 Database onwards). So is there reasons to use explicit cursors at all? Off course. Explicit cursor can still be more efficient and they off course offer much programmatic control.
Implicit cursor
Implicit cursors are used when you need to retrieve single row from database. If you want to retrieve more than one row, then you must use either an explicit cursor or bulk collect.
Here one example of implicit cursor usage:
- SET serveroutput on;
- DECLARE
- PROCEDURE find_employee (employee_id_v employees.employee_id%TYPE)
- IS
- --Record in which we will fetch entire row.
- emp_rec employees%ROWTYPE;
- BEGIN
- --Begining of implicit cursor statement.
- SELECT *
- INTO emp_rec --Fetch into record.
- FROM employees
- WHERE employee_id = employee_id_v;
- --Write result.
- DBMS_OUTPUT.put_line (emp_rec.employee_id || ' ' || emp_rec.first_name);
- --Catch exception when there is no such employee.
- EXCEPTION
- WHEN NO_DATA_FOUND
- THEN
- DBMS_OUTPUT.put_line ('Unknown employee with id: ' || employee_id_v);
- END find_employee;
- BEGIN
- find_employee (101);
- find_employee (102);
- --This one will produce exeption (OK, only if you do not have employee with id 1021).
- find_employee (1021);
- END;
We encapsulate query with function (this is _aways_ a good idea). This function print employee information from database to output. Also we introduce some exception handling (when
no employee is found).
Because PL/SQL is so tightly integrated with the Oracle database, you can easily retrieve complex datatypes (entire row for example - as we did in our example).
You can see that using implicit cursor is quite simple (with basic understanding of SQL) we just create simple select statement and insert rowset into record (that we declared as local variable).
Explicit cursor
Explicit cursor is explicitly defined in the declaration section. With explicit cursor, you have complete control over the different PL/SQL steps involved in retrieving information from the database. You decide when to open, when fetch and how many records and when to close cursor. Information about the current state of cursor is available through examination of cursor attributes.
Example:
- SET SERVEROUTPUT on;
- DECLARE
- PROCEDURE get_all_employees
- IS
- --Employee record variable.
- employee_rec employees%ROWTYPE;
- --Cursor variable for explicit use.
- CURSOR employee_cur
- IS
- SELECT *
- FROM employees;
- BEGIN
- --Open cursor so you can use it.
- OPEN employee_cur;
- --Go through all employees.
- LOOP
- --Load current row from cursor into employee record.
- FETCH employee_cur
- INTO employee_rec;
- --Loop until cursor attribute signals that no rows are found.
- EXIT WHEN employee_cur%NOTFOUND;
- DBMS_OUTPUT.put_line ( employee_rec.employee_id
- || ', '
- || employee_rec.first_name
- );
- END LOOP;
- CLOSE employee_cur;
- EXCEPTION
- --Remember to close cursor even if there was some error.
- WHEN OTHERS
- THEN
- IF employee_cur%ISOPEN
- THEN
- CLOSE employee_cur;
- END IF;
- END get_all_employees;
- BEGIN
- get_all_employees ();
- END;
This PL/SQL block performs following:
Declare the cursor.
Declare а record based on that cursor.
Open the cursor.
Fetch rows until there are no rows left.
Close cursor.
Handle exception and close cursor if it is not closed.
Copied from link
No comments:
Post a Comment