Implicit cursors
Implicit cursors are automatically created and used by Oracle every time you issue a Select statement in PL/SQL. If you use an implicit cursor, Oracle will perform the open, fetches, and close for you automatically. Implicit cursors are used in statements that return only one row. If the SQL statement returns more than one row, an error will occur.
The Oracle server implicitly opens a cursor to process each SQL statement not associated with an explicitly declared cursor. PL/SQL allows you to refer to the most recent implicit cursor as the SQL cursor.
For a long time there have been debates over the relative merits of implicit cursors and explicit cursors. The short answer is that implicit cursors are faster and result in much neater code so there are very few cases where you need to resort to explicit cursor.
The process of an implicit cursor is as follows:
The implicit cursor is used to process INSERT, UPDATE, DELETE, and SELECT INTO statements. Oracle automatically performs the OPEN, FETCH, and CLOSE operations, during the processing of an implicit cursor.
Example 1 of an Implicit cursors
In the following PL/SQL code block, the select statement makes use of an implicit cursor:
Begin
Update emp Where 1=2;
Dbms_output.put_line (sql%rowcount ||’ ‘|| ‘ rows are affected by the update statement’);
End;
SELECT SUM (sal) INTO TOTAL
FROM emp
WHERE depno = 10;
Another Example of an Implicit cursor
The following single-row query calculates and returns the total salary for a department. PL/SQL creates an implicit cursor for this statement:
SELECT SUM (salary) INTO department_total
FROM employee
WHERE department_number = 10;
PL/SQL provides some attributes, which allow you to evaluate what happened when the implicit cursor was last used. You can use these attributes in PL/SQL statements like functions but you cannot use then within SQL statements.
An Example of PL/SQL Attribute
DECLARE
rows_deleted NUMBER;
BEGIN
DELETE * FROM emp;
rows_deleted := SQL%ROWCOUNT;
END;
The implicit cursor has the following drawbacks:
Please see this link to know if Implicit cursors are fast or Explicit cursors.
Explicit Cursors
Programmers create explicit cursors, and with these you can do operations on a set of rows, which can be processed one by one. You use explicit cursors when you are sure that the SQL statement will return more than one row. You have to declare an explicit cursor in the declare section at the beginning of the PL/SQL block.
Use explicit cursors to individually process each row returned by a multiple-row SELECT statement.
Explicit cursor functions:
Declare: This clause initializes the cursor into memory.
Open: The previously declared cursor is now open and memory is allotted.
Fetch: The previously declared and opened cursor can now access data;
Close: The previously declared, opened, and fetched cursor is closed, which also releases memory allocation.
Below is a small example of an Explicit cursor:
SQL> set serveroutput on;
SQL> Declare
2 Cursor empcursor(empn in number)
3 Is select * from emp where empno=empn;
4
5 empvar emp%rowtype;
6 Begin
7 Dbms_output.put_line('Getting records for employee no. 7521');
8 Open empcursor(7521);
9 Loop
10 Fetch empcursor into empvar;
11 Exit when empcursor%notfound;
12 Dbms_output.put_line('emp name : ' || empvar.ename);
13 Dbms_output.put_line('emp salary : ' || empvar.sal);
14 End loop;
15 Close empcursor;
16 End;
17 /
Getting records for employee no. 7521
emp name : WARD
emp salary : 1250
PL/SQL procedure successfully completed.
Explicit Cursor Attributes
Here are the main cursor attributes:
Few more examples of Explicit Cursors:
Example 1 of an Explicit Cursor:
An example to retrieve the first 10 employees one by one.
SET SERVEROUTPUT ON
DECLARE
v_empno employees.employee_id%TYPE;
v_ename employees.last_name%TYPE;
CURSOR emp_cursor IS
SELECT employee_id, last_name
FROM employees;
BEGIN
OPEN emp_cursor;
FOR i IN 1..10 LOOP
FETCH emp_cursor INTO v_empno, v_ename;
DBMS_OUTPUT.PUT_LINE (TO_CHAR(v_empno)
||' '|| v_ename);
END LOOP;
Close emp_cursor
END ;
Example 2 of an Explicit Cursor:
DECLARE
CURSOR csr_ac (p_name VARCHAR2) IS
SELECT empno, name, sal
FROM employee
WHERE name LIKE '%p_name%';
BEGIN
FOR rec_ac IN csr_ac ('LE')
LOOP
DBMS_OUTPUT.PUT_LINE(rec_ac.empno || ' ' ||rec_ac.name || ' '||v_sal);
END LOOP ;
CLOSE csr_ac;
END;
Example 3 of an Explicit Cursor:
Another way of writing the above code, is to use the basic loop and the SQL%NOTFOUND cursor, as shown in the following.
DECLARE
CURSOR csr_ac (p_name VARCHAR2) IS
SELECT empno, ename, sal
FROM emp
WHERE ename LIKE '%SMITH%';
v_a emp.empno%TYPE;
v_b emp.ename%TYPE;
v_c emp.sal%TYPE;
BEGIN
OPEN csr_ac('');
LOOP
FETCH csr_ac INTO v_a, v_b, v_c;
EXIT WHEN csr_ac%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_a || ' ' || v_b || ' '||v_c);
END LOOP;
CLOSE csr_ac;
END;
Implicit cursors are automatically created and used by Oracle every time you issue a Select statement in PL/SQL. If you use an implicit cursor, Oracle will perform the open, fetches, and close for you automatically. Implicit cursors are used in statements that return only one row. If the SQL statement returns more than one row, an error will occur.
The Oracle server implicitly opens a cursor to process each SQL statement not associated with an explicitly declared cursor. PL/SQL allows you to refer to the most recent implicit cursor as the SQL cursor.
For a long time there have been debates over the relative merits of implicit cursors and explicit cursors. The short answer is that implicit cursors are faster and result in much neater code so there are very few cases where you need to resort to explicit cursor.
The process of an implicit cursor is as follows:
- Whenever an SQL statement is executed, any given PL/SQL block issues an implicit cursor, as long as an explicit cursor does not exist for that SQL statement.
- A cursor is automatically associated with every DML statement (UPDATE, DELETE, and INSERT).
- All UPDATE and DELETE statements have cursors those recognize the set of rows that will be affected by the operation.
- An INSERT statement requires a place to accept the data that is to be inserted in the database; the implicit cursor fulfills this need.
- The most recently opened cursor is called the “SQL%” Cursor.
The implicit cursor is used to process INSERT, UPDATE, DELETE, and SELECT INTO statements. Oracle automatically performs the OPEN, FETCH, and CLOSE operations, during the processing of an implicit cursor.
Example 1 of an Implicit cursors
In the following PL/SQL code block, the select statement makes use of an implicit cursor:
Begin
Update emp Where 1=2;
Dbms_output.put_line (sql%rowcount ||’ ‘|| ‘ rows are affected by the update statement’);
End;
SELECT SUM (sal) INTO TOTAL
FROM emp
WHERE depno = 10;
Another Example of an Implicit cursor
The following single-row query calculates and returns the total salary for a department. PL/SQL creates an implicit cursor for this statement:
SELECT SUM (salary) INTO department_total
FROM employee
WHERE department_number = 10;
PL/SQL provides some attributes, which allow you to evaluate what happened when the implicit cursor was last used. You can use these attributes in PL/SQL statements like functions but you cannot use then within SQL statements.
%ROWCOUNT | The number of rows processed by a SQL statement. |
%FOUND | TRUE if at least one row was processed. |
%NOTFOUND | TRUE if no rows were processed. |
%ISOPEN | TRUE if cursor is open or FALSE if cursor has not been opened or has been closed. Only used with explicit cursors. |
An Example of PL/SQL Attribute
DECLARE
rows_deleted NUMBER;
BEGIN
DELETE * FROM emp;
rows_deleted := SQL%ROWCOUNT;
END;
The implicit cursor has the following drawbacks:
- It is less efficient than an explicit cursor.
- It is more vulnerable to data errors.
- It gives you less programmatic control.
Please see this link to know if Implicit cursors are fast or Explicit cursors.
Explicit Cursors
Programmers create explicit cursors, and with these you can do operations on a set of rows, which can be processed one by one. You use explicit cursors when you are sure that the SQL statement will return more than one row. You have to declare an explicit cursor in the declare section at the beginning of the PL/SQL block.
Use explicit cursors to individually process each row returned by a multiple-row SELECT statement.
Explicit cursor functions:
- Can process beyond the first row returned by the query, row by row
- Keep track of which row is currently being processed
- Allow the programmer to manually control explicit cursors in the PL/SQL block
Declare: This clause initializes the cursor into memory.
Open: The previously declared cursor is now open and memory is allotted.
Fetch: The previously declared and opened cursor can now access data;
Close: The previously declared, opened, and fetched cursor is closed, which also releases memory allocation.
Below is a small example of an Explicit cursor:
SQL> set serveroutput on;
SQL> Declare
2 Cursor empcursor(empn in number)
3 Is select * from emp where empno=empn;
4
5 empvar emp%rowtype;
6 Begin
7 Dbms_output.put_line('Getting records for employee no. 7521');
8 Open empcursor(7521);
9 Loop
10 Fetch empcursor into empvar;
11 Exit when empcursor%notfound;
12 Dbms_output.put_line('emp name : ' || empvar.ename);
13 Dbms_output.put_line('emp salary : ' || empvar.sal);
14 End loop;
15 Close empcursor;
16 End;
17 /
Getting records for employee no. 7521
emp name : WARD
emp salary : 1250
PL/SQL procedure successfully completed.
Explicit Cursor Attributes
Here are the main cursor attributes:
%ISOPEN | It returns TRUE if cursor is open, and FALSE if it is not. |
%FOUND | It returns TRUE if the previous FETCH returned a row and FALSE if it did not. |
%NOTFOUND | It returns TRUE if the previous FETCH did not return a row and FALSE if it did. |
%ROWCOUNT | It gives you the number of rows the cursor fetched so far. |
Few more examples of Explicit Cursors:
Example 1 of an Explicit Cursor:
An example to retrieve the first 10 employees one by one.
SET SERVEROUTPUT ON
DECLARE
v_empno employees.employee_id%TYPE;
v_ename employees.last_name%TYPE;
CURSOR emp_cursor IS
SELECT employee_id, last_name
FROM employees;
BEGIN
OPEN emp_cursor;
FOR i IN 1..10 LOOP
FETCH emp_cursor INTO v_empno, v_ename;
DBMS_OUTPUT.PUT_LINE (TO_CHAR(v_empno)
||' '|| v_ename);
END LOOP;
Close emp_cursor
END ;
Example 2 of an Explicit Cursor:
DECLARE
CURSOR csr_ac (p_name VARCHAR2) IS
SELECT empno, name, sal
FROM employee
WHERE name LIKE '%p_name%';
BEGIN
FOR rec_ac IN csr_ac ('LE')
LOOP
DBMS_OUTPUT.PUT_LINE(rec_ac.empno || ' ' ||rec_ac.name || ' '||v_sal);
END LOOP ;
CLOSE csr_ac;
END;
Example 3 of an Explicit Cursor:
Another way of writing the above code, is to use the basic loop and the SQL%NOTFOUND cursor, as shown in the following.
DECLARE
CURSOR csr_ac (p_name VARCHAR2) IS
SELECT empno, ename, sal
FROM emp
WHERE ename LIKE '%SMITH%';
v_a emp.empno%TYPE;
v_b emp.ename%TYPE;
v_c emp.sal%TYPE;
BEGIN
OPEN csr_ac('');
LOOP
FETCH csr_ac INTO v_a, v_b, v_c;
EXIT WHEN csr_ac%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_a || ' ' || v_b || ' '||v_c);
END LOOP;
CLOSE csr_ac;
END;
RSS Feed |
No comments:
Post a Comment