This Oracle tutorial explains how to declare a cursor in Oracle/PLSQL with syntax and examples.
A cursor is a SELECT statement that is defined within the declaration section of your PLSQL code. We'll take a look at three different syntaxes to declare a cursor.
Declaring a cursor without any parameters is the simplest cursor. Let's take a closer look.
The syntax for a cursor without parameters in Oracle/PLSQL is:
For example, you could define a cursor called c1 as below.
The result set of this cursor is all course_numbers whose course_name matches the variable called name_in.
Below is a function that uses this cursor.
As we get more complicated, we can declare cursors with parameters.
The syntax for a cursor with parameters in Oracle/PLSQL is:
For example, you could define a cursor called c2 as below.
The result set of this cursor is all course_numbers whose subject_id matches the subject_id passed to the cursor via the parameter.
Finally, we can declare a cursor with a return clause.
The syntax for a cursor with a return clause in Oracle/PLSQL is:
For example, you could define a cursor called c3 as below.
The result set of this cursor is all columns from the course_tbl where the subject is Mathematics.