0% found this document useful (0 votes)
5 views

What is Parameterized cursor

Parameterized cursors in PL/SQL are explicit cursors that accept parameters, allowing for more flexible and reusable queries by passing different values to the WHERE clause. They avoid scoping issues by not tying the result set to specific variables, making them useful in nested blocks. The syntax for declaring and opening a parameterized cursor is similar to that of a simple cursor, with the addition of parameters in parentheses.

Uploaded by

Sushma Borkar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

What is Parameterized cursor

Parameterized cursors in PL/SQL are explicit cursors that accept parameters, allowing for more flexible and reusable queries by passing different values to the WHERE clause. They avoid scoping issues by not tying the result set to specific variables, making them useful in nested blocks. The syntax for declaring and opening a parameterized cursor is similar to that of a simple cursor, with the addition of parameters in parentheses.

Uploaded by

Sushma Borkar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

What is Parameterized cursor?

Unlike simple explicit cursor, parameterized cursors accept values as parameter. You
specify the list of parameters separated by comma (,) while declaring the cursor and
supply the corresponding argument for each parameter in the list while opening the
cursor.

Definition:
Cursor parameter can be appropriately defined as an explicit cursor that accepts
arguments from the user in the form of parameter.

PL/SQL Parameterized cursor define only datatype of parameter and not need to
define its length.
Default values is assigned to the Cursor parameters. and scope of the parameters are
locally.
Parameterized cursors are also saying static cursors that can passed parameter value
when cursor are opened.

Syntax of Parameterized Cursor in Oracle Database

CURSOR cur _ name (parameter list) IS SELECT statement;


Syntax of declaring a cursor parameter is pretty similar to that of the simple cursor
except the addition of parameters enclosed in the parenthesis.

OPEN cur _ name (argument list)


You have to provide corresponding arguments for each parameter that are specified
during the declaration. Rest of the steps are the same as that of the simple cursor.

There are few things which you have to take care of while specifying the parameters
in your explicit cursor.

 In case of multiple parameters, always separate parameters and the


corresponding arguments in the list from each other using comma (,).
 You can specify as many parameters as you need just make sure to include
an argument in parameter list for each parameter when you open the cursor.
 While specifying a parameter during the declaration of the explicit cursor
only specify the data type not the data width.
Advantages of Parameterized Cursors

Makes the cursor more reusable

You can use a parameter and then pass different values to the WHERE clause each
time a cursor is opened instead of hardcoding a value into the WHERE clause of a
query to select particular information.

Avoids scoping problems

When you pass parameters instead of hardcoding the values, the result set for that
cursor is not tied to a specific variable in a program or block. Therefore in case your
program has nested blocks, you can define the cursor at a higher-level (enclosing)
block and use it in any of the sub-blocks with variables defined in those local blocks.

When do we need a parameterized cursor?

You must be wondering when we need a cursor with parameters in our PL/SQL.

The simplest answer is whenever you need to use your cursor in more than one place
with different values for the same WHERE clause of your SELECT statement.

If you can add something to this and have another idea for using a parameterized
cursor. Then I am always open to listening to your thoughts do make sure to share it
with me on my Facebook or twitter.

Example of Parameterized cursor.

SET SERVEROUTPUT ON;

DECLARE

v_name VARCHAR2 (30);

--Declare Cursor

CURSOR p_cur_RebellionRider (var_e_id VARCHAR2) IS

SELECT first_name FROM EMPLOYEES

WHERE employee_id<var_e_id;

BEGIN

OPEN p_cur_RebellionRider (105);


LOOP

FETCH p_cur_RebellionRider INTO v_name;

EXIT WHEN p_cur_RebellionRider%NOTFOUND;

DBMS_OUTPUT.PUT_LINE(v_name );

END LOOP;

CLOSE p_cur_RebellionRider;

END;

%TYPE : Used to declare a field with the same type as that of a specified table's
column.

DECLARE
vEmployeeNameEmployee.Name%TYPE;
BEGIN
SELECT Name
INTO vEmployeeName
FROM Employee
WHERE RowNum = 1;

DBMS_OUTPUT.PUT_LINE(vEmployeeName);
END;
/
%ROWTYPE: Used to declare a record with the same types as found in the specified
table, view or cursor (= multiple columns).

DECLARE
rEmployeeEmployee%ROWTYPE;
BEGIN
rEmployee.Name := 'Matt';
rEmployee.Age := 31;

DBMS_OUTPUT.PUT_LINE(rEmployee.Name);
DBMS_OUTPUT.PUT_LINE(rEmployee.Age);
END;
/

You might also like