How to store result of a function which will return sysrefcursor? - sql

Scenario: there is an procedure inside which we have a cursor.
I need to call a function which will take an input from that cursor value and will return SYS_REFCURSOR.
I need to store that result of function in a different variable/cursor & need to return this value from procedure as out parameter.
I am using Oracle 11g.
How can I proceed?
PFB My Approach:
create or replace procedure prc_test
(p_dept_id in number,
c_detail out sysrefcursor)--need to add extra out parameter
as
var1 varchar2(200) :=null;
begin
open c_detail for
select -1 from dual;
if p_dept_id is not null then
open c_detail for
select emp_no from emp
where dept_id=p_dept_id;
--i need to retrn value of 'get_emp_dtls' function as out parameter.
end if;
end procedure;
/
Function to be called
CREATE OR REPLACE FUNCTION get_emp_dtls
(p_emp_no IN EMP.EMP_NO%TYPE)
RETURN SYS_REFCURSOR
AS
o_cursor SYS_REFCURSOR;
BEGIN
OPEN o_cursor FOR
SELECT
ENAME,
JOB
FROM emp
WHERE EMP_NO = p_emp_no;
RETURN o_cursor;
-- exception part
END;
/

Here is your function that takes a varchar2 variable and returns A refcursor( weakly typed).
CREATE OR replace FUNCTION fn_return_cur(v IN VARCHAR2)
RETURN SYS_REFCURSOR
IS
c1 SYS_REFCURSOR;
BEGIN
OPEN c1 FOR
SELECT 'ABC'
FROM dual
WHERE 'col1' = v;
RETURN c1;
END;
/
Here is the procedure that has a cursor value passed as argument to function and the returned cursor passed as OUT argument.
CREATE OR replace PROCEDURE Pr_pass_out_cur(v_2 OUT SYS_REFCURSOR)
IS
func_arg VARCHAR2(3);
other_arg VARCHAR2(3);
CURSOR c_2 IS
SELECT 'ABC' col1,
'DEF' col2
FROM dual;
BEGIN
LOOP
FETCH c_2 INTO func_arg, other_arg;
EXIT WHEN c_2%NOTFOUND;
v_2 := Fn_return_cur(func_arg);
END LOOP;
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
/
Let me know your feedback.

Related

How can I return a SELECT query with an oracle function?

I need to create a function that allows me to return the same result as a SELECT query and that contains pl/sql code.
I tried something really simple :
create or replace FUNCTION test
RETURN SYS_REFCURSOR
IS
l_rc SYS_REFCURSOR;
BEGIN
OPEN l_rc
FOR SELECT *
FROM my_table;
RETURN l_rc;
END;
But when I call my function with SELECT test from dual;, I get all result from my_table in a single cell instead of having each columns separated.
Is there a way of doing what I want ?
Ideally, I want a view but there seems to be no way of adding logical conditions with them.
the function has to be pipelined. For example :
TYPE MyType IS RECORD(ID NUMBER);
TYPE MyTableType IS TABLE OF MyType;
Function MyFunction(Arguments) return MyTableType pipelined is
Cursor Cur is select * from whetever;
R Cur%rowtype;
Begin
Open cur;
loop
fetch Cur into R;
exit when Cur%notfound;
pipe row(R);
End loop;
Close cur;
End MyFunction;
Then you can call it via :
select * from table(MyFunction(Arguments));
The simplest way is to leave the function as it is and only call it properly:
create table my_table (id, memo) as
select 1, 'some memo' from dual
/
create or replace function MyTableById (id int) return sys_refcursor is
rc sys_refcursor;
begin
open rc for
select * from my_table where id=MyTableById.id;
return rc;
end;
/
var rc refcursor
exec :rc := MyTableById (1);
print rc
ID MEMO
---------- ---------
1 some memo

How to return null in OUT SYS_REFCURSOR

I have a procedure in the package which returns a SYS_REFCURSOR, i want to return null or empty based on a condition, not sure how can i do it.
PROCEDURE test( id_number IN VARCHAR2,
resultIN OUT SYS_REFCURSOR) AS
BEGIN
if true then
OPEN resultIN FOR
SELECT
fieldsValue
from TableName;
ELSE
Return empty resultIN ;
END IF;
END;
This is not working for me. How can I do the same.
You can use fake query as follows:
OPEN resultIN FOR
select * from dual where 1=2;
-- if you want one row also then use
OPEN resultIN FOR
select case when 1=2 then 1 end as dummy_row from dual;
It is important to use OPEN resultIN FOR otherwise application which is going to use it will directly throw closed cursor error. (Cursor is not opened.)
You can simply assign NULL to the refcursor like this:
PROCEDURE test( id_number IN VARCHAR2,
resultIN OUT SYS_REFCURSOR) AS
BEGIN
if false then
OPEN resultIN FOR
SELECT dummy
from dual;
ELSE
resultIN := null;
END IF;
END;

Invalid Data Type error while casting PL/SQL Array to Table

I have a PL/SQL stored proc which needs to return a RefCursor type object as output parameter.
PROCEDURE usp_appnt_stts_driver_wraper2
( in_req_src_system_id IN NUMBER,
in_req_user_info IN VARCHAR2,
out_response_rec1 OUT SYS_REFCURSOR)
At the end of the SP, I am able to return Hard Coded values to my front end by using a Select Statement.
OPEN out_response_rec1 FOR
SELECT 'data1', 'data2', 'data3', 'data 4' FROM DUAL;
This works fine. But I need to send the data which I am getting from an Array.
The Array is populated like this,
FOR g_index IN g_slsnetoutbndarr.FIRST..g_slsnetoutbndarr.LAST
LOOP
out_response_rec.EXTEND;
IF g_SlsnetOutbndArr(g_index).Rectypdesc IS NOT NULL THEN
out_response_rec(g_index).Rectypdesc := g_SlsnetOutbndArr(g_index).Rectypdesc ;
out_response_rec(g_index).Recdetltcode := g_SlsnetOutbndArr(g_index).Recdetltcode;
out_response_rec(g_index).RecDetlDesc := g_SlsnetOutbndArr(g_index).RecDetlDesc ;
END IF;
END LOOP;
So at the end of this code, the Array Object out_response_rec has all the values I need.
But How do I transfer these values in the RefCursor output parameter?
Update 1
I have tried to create a new data type in the Package specification.
TYPE SlsnetOutbndRec IS RECORD(
Rectypdesc VARCHAR2(30),
Recdetltcode NUMBER,
RecDetlDesc VARCHAR2(130));
TYPE SlsnetOutbndTabArr IS TABLE OF SlsnetOutbndRec;
Finally I have tried to Cast the Array to table in my SP as
OPEN out_response_rec_result FOR
SELECT * FROM TABLE (Cast(out_response_rec AS SlsnetOutbndTabArr));
But this is throwing an Invalid Data Type error. The SP does not recognize the new data types I created.
So at the end of this code, the Array Object out_response_rec has all
the values I need.
But How do I transfer these values in the RefCursor output parameter?
As I could understand, you wanted to use a SYS_REFCUSOR to get the output of a Query plus the values that are in collection which is being populated by another Procedure. I have come up with a solution for your requirement see below inline explaination:
--Create a Object in Place of Record since we cannot use a PLSQL scope compnenet in SQL scope in Oracle 11g and below
CREATE OR REPLACE TYPE SlsnetOutbndRec IS OBJECT
(
Rectypdesc VARCHAR2 (30),
Recdetltcode NUMBER,
RecDetlDesc VARCHAR2 (130)
);
--Create a table type of the Object
CREATE OR REPLACE TYPE SlsnetOutbndTabArr IS TABLE OF SlsnetOutbndRec;
/
--Procedure
CREATE OR REPLACE PROCEDURE combined_rslt (var OUT SYS_REFCURSOR)
AS
v_var SlsnetOutbndTabArr := SlsnetOutbndTabArr ();
l_var SlsnetOutbndTabArr := SlsnetOutbndTabArr ();
BEGIN
--Populating the collection
v_var.EXTEND;
v_var (1) := SlsnetOutbndRec ('ABC', 1, 'A');
OPEN VAR FOR
SELECT 'CDE', 2, 'B' FROM DUAL
UNION ALL -- Combining the result of collection with the result of query
SELECT *
FROM TABLE (v_var) t;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (SQLERRM);
END;
Execution :
DECLARE
x SYS_REFCURSOR;
a VARCHAR2 (30);
b NUMBER;
c VARCHAR2 (130);
BEGIN
combined_rslt (var => x);
LOOP
FETCH x INTO a, b, c;
EXIT WHEN x%NOTFOUND;
DBMS_OUTPUT.put_line (a || ' ' || b || ' ' || c);
END LOOP;
END;
Results:
SQL> /
CDE 2 B
ABC 1 A
PL/SQL procedure successfully completed.
You need to create the types in the SQL scope (not the PL/SQL scope) if you want to use them in SQL statements (in versions prior to Oracle 12):
CREATE TYPE SlsnetOutbndRec IS OBJECT(
Rectypdesc VARCHAR2(30),
Recdetltcode NUMBER,
RecDetlDesc VARCHAR2(130)
)
/
CREATE TYPE SlsnetOutbndTabArr IS TABLE OF SlsnetOutbndRec
/
Then you can use it in your procedure (assuming your 3rd party data type is a collection or a VARRAY):
PROCEDURE usp_appnt_stts_driver_wraper2(
in_req_src_system_id IN NUMBER,
in_req_user_info IN VARCHAR2,
out_response_rec_result OUT SYS_REFCURSOR
)
IS
out_response_rec SlsnetOutbndTabArr := SlsnetOutbndTabArr();
g_slsnetoutbndarr ThirdPartyDataType := Get_From_3rd_party_Package();
BEGIN
FOR i IN 1 .. g_slsnetoutbndarr.COUNT LOOP
IF g_SlsnetOutbndArr(i).Rectypdesc IS NOT NULL THEN
out_response_rec.EXTEND;
out_response_rec := SlsnetOutbndRec(
g_SlsnetOutbndArr(i).Rectypdesc,
g_SlsnetOutbndArr(i).Recdetltcode,
g_SlsnetOutbndArr(i).RecDetlDesc
);
END IF;
END LOOP;
OPEN out_response_rec_result FOR
SELECT *
FROM TABLE( out_response_rec );
END;
If the 3rd party data type is an associative array then:
...
IS
out_response_rec SlsnetOutbndTabArr := SlsnetOutbndTabArr();
g_slsnetoutbndarr ThirdPartyDataType := Get_From_3rd_party_Package();
i ThirdPartyIndexType := g_slsnetoutbndarr.FIRST;
BEGIN
WHILE i IS NOT NULL LOOP
IF g_SlsnetOutbndArr(i).Rectypdesc IS NOT NULL THEN
out_response_rec.EXTEND;
out_response_rec := SlsnetOutbndRec(
g_SlsnetOutbndArr(i).Rectypdesc,
g_SlsnetOutbndArr(i).Recdetltcode,
g_SlsnetOutbndArr(i).RecDetlDesc
);
END IF;
i := g_slsnetoutbndarr.NEXT(i);
END LOOP;
...

inconsistent datatypes: when returning table from cursor in a package function - ORACLE

I have the following package. I am trying to fill the records inside the function from a cursor and return the record. I am unsure of how to assign the rows from the cursor into the record variable. I need to return the record so that I could create a materialized view from it.
CREATE OR REPLACE PACKAGE pkg_contrator_of_consultant AS
TYPE cst_record IS RECORD(
consultant_id NUMBER(10));
TYPE cst_id_type IS TABLE OF cst_record;
FUNCTION fnc_get_contractor_id(cst_username IN VARCHAR2)
RETURN cst_id_type
PIPELINED;
END;
CREATE OR REPLACE PACKAGE BODY pkg_contrator_of_consultant AS
FUNCTION fnc_get_contractor_id(cst_username IN VARCHAR2 )
RETURN cst_id_type
PIPELINED IS
V_cursor_contracotr_id cst_id_type;
CURSOR cursor_contract_name IS
SELECT plc.FK2_CONTRACTOR_ID
FROM lds_consultant cons
INNER JOIN lds_account acc on cons.consultant_id = acc.fk1_consultant_id
INNER JOIN lds_placement plc on acc.account_id = plc.FK1_ACCOUNT_ID
WHERE UPPER(cons.USERNAME) = UPPER(cst_username)
AND UPPER(plc.PLT_TO_PERMANENT) = UPPER('Y');
V_contracotr_id cst_id_type;
BEGIN
FOR rec IN cursor_contract_name LOOP
V_contracotr_id := rec.fk2_contractor_id;
SELECT V_contracotr_id INTO V_cursor_contracotr_id FROM DUAL;
dbms_output.put_line('cst_username : '||cst_username||' V_contracotr_id :'||V_contracotr_id);
END LOOP;
PIPE ROW (V_cursor_contracotr_id);
RETURN;
END fnc_get_contractor_id;
END;
/
In the line
V_contracotr_id := rec.fk2_contractor_id;
It gives the error "inconsistent datatypes: expected UDT got NUMBER" when the column selected by the cursor is of NUMBER type.
FK2_CONTRACTOR_ID NUMBER
Try this:
out_rec cst_record;
CURSOR C1 IS
SELECT ...;
BEGIN
open c1;
LOOP
FETCH c1 INTO out_rec;
exit when c1%notfound;
PIPE ROW(out_rec);
END LOOP;
close c1;
RETURN;
END fnc_get_contractor_id;
UPDATED CODE:
CREATE OR REPLACE PACKAGE pkg_contrator_of_consultant AS
TYPE cst_record IS RECORD(
consultant_id NUMBER(10));
TYPE cst_id_type IS TABLE OF cst_record;
FUNCTION fnc_get_contractor_id(cst_username IN VARCHAR2)
RETURN cst_id_type
PIPELINED;
END;
/
CREATE OR REPLACE PACKAGE BODY pkg_contrator_of_consultant AS
FUNCTION fnc_get_contractor_id(cst_username IN VARCHAR2 )
RETURN cst_id_type
PIPELINED IS
CURSOR c1 IS
SELECT plc.FK2_CONTRACTOR_ID
FROM lds_consultant cons
INNER JOIN lds_account acc on cons.consultant_id = acc.fk1_consultant_id
INNER JOIN lds_placement plc on acc.account_id = plc.FK1_ACCOUNT_ID
WHERE UPPER(cons.USERNAME) = UPPER(cst_username)
AND UPPER(plc.PLT_TO_PERMANENT) = UPPER('Y');
out_rec cst_record;
BEGIN
open c1;
LOOP
FETCH c1 INTO out_rec;
exit when c1%notfound;
PIPE ROW(out_rec);
END LOOP;
close c1;
RETURN;
END fnc_get_contractor_id;
END;
/

how to make variable hold the value taken from one procedure to be used in another procedure in plsql

I have a package with two procedures
in same package.
p_set_values (pn_id_number IN number)
p_get_values (prc_records OUT sys_refcursor)
p_set_values (pn_id_number IN number)
inserts data in to my_table where id_number = pn_id_number
p_get_values (prc_records OUT sys_refcursor) - this procedure has to select the value from my_table where id_number = pn_id_number (Note: same id number which is used to insert the value ,now used to set the values inserted.)
I have declared package level variable and assigned as ln_id_number = pn_id_number.
Now when using this 'ln_id_number' in second procedure, the value of ln_id_number is nothing when checked using dbms_putput.putline(ln_id_number)
Please help me to do this,
Procedures are as follows
CREATE OR REPLACE PACKAGE BODY pck_exit_info
IS
ln_id_number po010.polref%TYPE;
PROCEDURE p_set_values(pn_id_number IN number
,pv_exit_option IN VARCHAR2)
IS
ln_system_date cs340.sysdte%TYPE;
lv_audaplcde package_audits.audit_application_code%TYPE;
ln_audstfno package_audits.audit_staff_number%TYPE;
ln_audupdid NUMBER;
lv_exit_option VARCHAR2(1);
BEGIN
ln_system_date := pck_system_context.f_get_system_date;
ln_id_number := pn_id_number;
dbms_output.put_line(ln_id_number);
SELECT AUDUPDID_SEQ.NEXTVAL INTO ln_audupdid FROM DUAL;
IF pv_exit_option = 'LE' THEN
lv_exit_option := 'L';
ELSE
lv_exit_option := 'S';
END IF;
SELECT audit_application_code,audit_staff_number
INTO lv_audaplcde,ln_audstfno
FROM package_audits
WHERE process = 'PCK_LEAVERS'
AND subprocess ='DEFAULT';
INSERT INTO my_table
VALUES(pn_id_number
,ln_system_date
,lv_exit_option
,ln_audupdid
,ln_system_date
,lv_audaplcde
,ln_audstfno);
END set_employer_exit_info;
PROCEDURE p_get_values(prc_records OUT SYS_REFCURSOR) IS
BEGIN
/*dbms_output.put_line('ID inside get employer');
dbms_output.put_line(ln_id_number);*/
OPEN prc_records FOR
SELECT *
FROM my_table
WHERE polref = ln_id_number;
-- CLOSE prc_policy;
END get_employer_exit_info;
END pck_exit_info;