OPEN CURSOR in Oracle PLSQL is used to open a cursor before starting to access the records or results fetched. Oracle OPEN Cursor statement allows us to use the records fetched by the cursor in a PLSQL Function, package or procedure.
Oracle PLSQL Syntax of OPEN CURSOR is:
OPEN cursor_name;
Let’s understand, how to use OPEN Cursor with the help of the below Oracle PLSQL function code:
1 | CREATE OR REPLACE FUNCTION GetSalary |
2 | IS |
3 | cur_sal NUMBER; |
4 | CURSOR cur_salary |
5 | IS |
6 | SELECT salary |
7 | FROM employee; |
8 | BEGIN |
9 | OPEN cur_salary; |
10 | FETCH cur_salary IN cur_sal; |
11 | IF cur_salary%NOTFOUND THEN |
12 | cur_sal := 100000; |
13 | END IF; |
14 | CLOSE cur_salary; |
15 | END ; |
The code OPEN cur_salary is used to open a cursor in plsql.
We must also make sure that once we open a cursor we have to close the cursor using CLOSE CURSOR statement.