Greens Technologys phone
Greens Technologys Whatsapp

ref cursor examples

Oracle Database PLSQL Tips by Greens Technologies May 08, 2020

PL/SQL Ref Cursors examples


A REF Cursor is a datatype that holds a cursor value in the same way that a VARCHAR2 variable will hold a string value.

A ref cursor is technically same as cursor and can be processed in the same fashion at the most basic level.

A ref cursor is defined at runtime and acts as a pointer to the result set of the select query with it is opened dynamically.

Ref Cursor can be passed/returned to another PL/SQL routine (function or procedure) or even can be returned to client from the Oracle Database Server.



Ref Cursors can be further categorized in 2 parts
1) Strongly Typed Ref Cursor
2) Weakly Typed Ref Cursor

 


When a return type is included while defining Ref Cursor it called Strongly Typed Ref Cursor. Structure of Strongly Typed Ref Cursor is known at the compile time and can only be associated with queries which return result-set of same structure.



Example of Strongly Typed Ref Cursor


create or replace function f_emp_Dept (p_deptno in number)
return sys_refcursor
is
type rec_row is record
(
employee_id employees.employee_id%type,
first_name employees.first_name%type,
manager_if employees.manager_id%type,
salary employees.salary%type,
department_name departments.department_name%type
);

type t_str_cursor is ref cursor return rec_row;

v_cur t_str_cursor;
begin
open v_cur for
select e.employee_id,e.firsT_name, e.manager_id,e.salary,d.department_name
from employees e, departments d
where d.department_id =e.department_id and d.department_id = p_deptno;

return v_cur;
end;
/


select f_emp_dept(90) from dual;

 

Weakly Typed Ref Cursor do not have return type. Weakly Typed ref cursor gives us great flexibility and can be associated with any query.

They can be directly created with predefined SYS_REFCURSOR type.



Example of Weakly Typed Ref Cursor

create or replace function f_emp_dept(p_deptno in number) return sys_refcursor is
v_cur sys_refcursor;
begin
open v_cur for
select e.employee_id, e.first_name, e.manager_id, e.salary, d.department_name
from employees e, departments d
where d.department_id = e.department_id
and d.department_id = p_deptno;

return v_cur;
end;
/

select f_get_emp_by_dept(90) from dual;

Testimonials
best Salesforce training center in chennai "Successfully completed Admin certification today. Thank you, Greens Technology team. Your practice tests are helpful to clear salesforce certification exams."

Salesforce training chennai ""Dear Karthick, Salesforce training has been outstanding. You have covered every aspect of the Salesforce which would boost the confidence of the attendee to dive into greater depths and face the interviews subsequently. I feel confident after attending the Salesforce course. I am sure you would be providing us your valuable high level guidence in our initial realtime project . Each of your session is a eye opener and it is a great joy to attend your Salesforce training. Thanks and Kindest Regards ""

Salesforce training classes in chennai "I thought I knew Salesforce until I took this course. My company sent me here against my will. It was definitely worth and I found out how many things I was doing wrong. Velmurugan is awesome. but i got a lot inspired by you. I will keep in touch and will always try to learn from you as much as I can. Thanks once again Velmurugan"
Salesforce training chennai I think this is the best Salesforce course I have taken so far. Well I am still in the process of learning new things but for me this learning process has become so easy only after I joined this course.. as Karthick is very organized and up to the point.. he knows what he is teaching and makes his point very clear by explaining numerous times. I would definitely recommend anyone who has any passion for Salesforce.