Oracle PL/SQL Ref Cursor Function Missing Char - oracle

When I run this code, the result should be 636790 but 63679 is only returned. Beating brains out on this one!!! Why the missing digit? Source table and column contain correct number of 636790.
create or replace package jt_types
as
type ttest is ref cursor;
end;
CREATE OR REPLACE FUNCTION jt_test
RETURN jt_types.ttest
IS
o_return jt_types.ttest;
BEGIN
OPEN o_return FOR
SELECT offn_id
FROM jt_offn_4
where offn_ID in (636790)
ORDER BY offn_id;
RETURN o_return;
END jt_test;
DECLARE
l_ids jt_types.ttest;
l_id NUMBER;
BEGIN
l_ids := jt_test;
LOOP
FETCH l_ids INTO l_id;
EXIT WHEN l_ids%NOTFOUND;
dbms_output.put_line('Rec: ' || l_id);
END LOOP;
CLOSE l_ids;
END;

The easiest way to do is to return sys_refcursor, here is the code:
create or replace
function jt_test
return sys_refcursor
is
o_return sys_refcursor;
begin
open o_return for
select offn_id
from jt_offn_4
where offn_ID = 636790;
return o_return;
end jt_test;
declare
l_ids sys_refcursor;
l_id number;
begin
l_ids := jt_test;
loop
fetch l_ids into l_id;
dbms_output.put_line('Rec: ' || l_id);
exit when l_ids%NOTFOUND;
end loop;
close l_ids;
end;

Related

how to return null if no data found from a function with return type sys_refcursor?

whenever i call this function it will return error like - invalid cursor
so, this is the body part of the package, the package specification also return a sys_refcursor. the error message shows that sys_refcursor invalid cursor.
FUNCTION SUMMARY
(i_name VARCHAR2, i_id VARCHAR2, i_label VARCHAR2)
RETURN SYS_REFCURSOR
IS
rc_result SYS_REFCURSOR;
v_sql CLOB;
Server VARCHAR2(100) := '#AI';
v_r_count NUMBER;
v_sp_count VARCHAR2(200);
V VARCHAR2(10);
BEGIN
EXECUTE IMMEDIATE 'select count(*) from run'||Server||' where id='''||i_id||''' and label='''||i_label||''' and re is not null' INTO v_r_count;
EXECUTE IMMEDIATE 'select table_name from all_tables where table_name like ''%se%'' and owner ='''||i_name||'''' INTO v_sp_count;
IF v_r_count > 0 THEN
BEGIN
v_sql := 'select RE from run'||v_Server||' where id='''||i_id||''' and label='''||i_label||'''';
END;
ELSIF v_sp_count IS NOT NULL THEN
BEGIN
v_sql := 'SELECT
process,
desc,
p_desc,
date
FROM
'||i_name||'.se
WHERE
errors = 1
AND lower(p_desc) LIKE lower(''%summary%'')';
END;
ELSE v_sql := NULL;
END IF;
OPEN result FOR v_sql;
RETURN result;
EXCEPTION
WHEN no_data_found THEN
RETURN NULL;
WHEN OTHERS THEN
dbms_output.put_line(SQLERRM);
END;
You can simply assign NULL to the refcursor like this: Example
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;

Looping Through Sys_Refcursor: Type Mismatch Found Between Cursor and INTO Varaiables

I have a function that returns a Sys_Refcursor. It looks like this:
Open l_cursor For
Select b.balance + b.arrears arrears_bucket
,b.levy + b.penalty levy_bucket
,b.supplementary_levy supp_bucket
,b.other_balance + b.other_penalty other_bucket
,b.arrears_balance + b.arrears_penalty + b.levy_balance + b.levy_penalty calculated_balance
From balances b
Where b.id = p_id;
Return l_cursor;
I have another function in which I want to call the above function, and loop through it. It looks like this:
Cursor l_cursor Is
Select balance_sel(p_id) From dual;
l_result1 Number;
l_result2 Number;
l_result3 Number;
l_result4 Number;
l_result5 Number;
Begin
Loop
Fetch l_cursor into l_result1, l_result2, l_result3, l_result4, l_result5;
EXIT WHEN l_cursor%notfound;
End Loop;
But I keep getting the error:
Error: PLS-00386: type mismatch found at 'L_RESULT1' between FETCH cursor and INTO variables
Line: 316
Text: Fetch l_cursor into l_result1, l_result2, l_result3, l_result4, l_result5;
I'm fetching numbers into numbers, so what it is the mismatch? How do I resolve it?
This seems like it should be simple but I am at a loss.
Thanks.
I think your problem is with the use of the cursor.
declare a variable of the type sys_refcursor and save the result of your function
declare
l_cursor Sys_Refcursor;
l_result1 Number;
l_result2 Number;
l_result3 Number;
l_result4 Number;
l_result5 Number;
Begin
...
l_cursor := balance_sel(p_id);
Loop
Fetch l_cursor into l_result1, l_result2, l_result3, l_result4, l_result5;
EXIT WHEN l_cursor%notfound;
End Loop;
end;
/

How to pass cursor rowtype between procedures in package

I have an explicit cursor in package, I create rowtype based on this cursor, how can I pass this rowtype between procedures?
My exmaple hung up when compile.
cursor cur(param) is
select * from dual where param = 1;
rec cur%rowtype;
procedure do_something_with_rec(p_rec in cur%rowtype)
is
begin
dbms_output.put_line(p_rec.dummy);
end;
procedure main(param)
is
begin
open cur(param);
loop
fetch into rec;
exit when cur%notfound;
end loop;
close cur;
do_something_with_rec(rec);
end;
You forgot to add parameter types in cursor, definition of procedure main and in fetch add cursor name. This package compiled and worked:
-- package
create or replace package p_test is
procedure main(param in number);
end p_test;
-- body
create or replace package body p_test is
cursor cur(param in number) is select * from dual where param = 1;
rec cur%rowtype;
procedure do_something_with_rec(p_rec in cur%rowtype) is
begin
dbms_output.put_line(p_rec.dummy);
end;
procedure main(param in number) is
begin
open cur(param);
loop
fetch cur into rec;
exit when cur%notfound;
end loop;
close cur;
do_something_with_rec(rec);
end;
end p_test;

how can i substr in oracle procedure

this is my procedure:
create or replace PROCEDURE testsub(factTName IN VARCHAR2)
IS
v_in_char VARCHAR2(100):='id,name,age,cjrq';
v_result VARCHAR2(200) :='';
begin
-- split v_in_char,expect cjrq
-- i want result like t.uuid=uuid and t.uuname=uuname and t.uuage=uuage
SYS.DBMS_OUTPUT.PUT_LINE(v_result);
end;
maybe something like this:
select substr(v_in_char,instr(v_in_char,',',1,3)+1) into v_result
from dual;
create or replace PROCEDURE testsub(factTName IN VARCHAR2)
IS
v_primy_keys VARCHAR2(200) :='';
cursor primy_key is
select field_en_name from BASE_FIELD_INFO where base_rpt_id=lower(factTName) and primy_key='true';
primy_key_row primy_key%ROWTYPE;
begin
for primy_key_row in primy_key loop
IF primy_key_row.field_en_name='CJRQ' THEN
DBMS_OUTPUT.PUT_LINE('');
ELSE
v_primy_keys:=v_primy_keys||'t.'||primy_key_row.field_en_name||'='||primy_key_row.field_en_name||' ';
END IF;
v_primy_keys:=v_primy_keys||' and ';
end loop;
v_primy_keys:=substr(v_primy_keys,1,length(v_primy_keys)-4);
dbms_output.put_line(v_primy_keys);
end;

Trying to understand how pl sql stored proc work and why my results differ

I have a simple stored proc...
create or replace
PROCEDURE GET_PERSON (aName VARCHAR2, p_data OUT sys_refcursor)
IS
BEGIN
OPEN p_data FOR SELECT * FROM people_table WHERE firstname = aName;
END;
However when I execute the stored proc it returns all of the records.
DECLARE
v_cur SYS_REFCURSOR;
v_1 number;
v_2 VARCHAR2(50);
v_3 VARCHAR2(200);
v_4 VARCHAR2(50);
v_5 VARCHAR2(50);
v_6 VARCHAR2(50);
BEGIN
GET_PERSON ('aaa#bbb.com', v_cur);
LOOP
FETCH v_cur INTO v_1, v_2, v_3, v_4, v_5, v_6;
EXIT WHEN v_cur%NOTFOUND;
dbms_output.put_line(v_2 || ' ' || v_3);
END LOOP;
CLOSE v_cur;
END;
If I run the simple statement
SELECT * FROM people_table WHERE firstname = 'aaa#bbb.com';
It correctly returns one record.
Why is the stored proc not behaving the same?
I found the issue..
My issue was name collision. When I altered the code above is when I noticed the issue. I originally has WHERE fistname = firstName. Once I changed the parameter to p_firstName as was well.

Resources