retrieve recordset with more than one column value using refcursor in oracle - plsql

am creating a package in pl/sql . with in this i declared the ref cursor . With in procedure am using select statement with multiple column name . but am not able to get the result.
here i attached my code. Help me to correct the error. Am new to pl/sql
code
CREATE OR REPLACE PACKAGE types AS
TYPE cursor_type IS REF CURSOR;
END Types;
/
CREATE OR REPLACE
PROCEDURE get_CDR_rs (p_no IN zkv.FLD_callingPartyNumber%TYPE,
CDR_recordset OUT SYS_REFCURSOR) AS
BEGIN
OPEN CDR_recordset FOR
SELECT FLD_callingPartyNumber,
FLD_dateTimeConnect
FROM CISCOCUIC_TBL
WHERE FLD_callingPartyNumber= p_no
ORDER BY FLD_callingPartyNumber,;
END get_CDR_rs;
/
SET SERVEROUTPUT ON SIZE 1000000
DECLARE
l_cursor SYS_REFCURSOR;
l_callingPartyNumber zkv.FLD_callingPartyNumber%TYPE;
l_dateTimeConnect zkv.FLD_dateTimeConnect%TYPE;
BEGIN
LOOP
FETCH l_cursor
INTO l_callingPartyNumber, l_dateTimeConnect;
EXIT WHEN l_cursor%NOTFOUND;
END LOOP;
CLOSE l_cursor;
END;
/
Error
9/41 PL/SQL: ORA-00936: missing expression
5/5 PL/SQL: SQL Statement ignored

First thing is there is a syntax error in the procedure. It should be
CREATE OR REPLACE
PROCEDURE get_CDR_rs (p_no IN zkv.FLD_callingPartyNumber%TYPE,
CDR_recordset OUT SYS_REFCURSOR) AS
BEGIN
OPEN CDR_recordset FOR
SELECT FLD_callingPartyNumber,
FLD_dateTimeConnect
FROM CISCOCUIC_TBL
WHERE FLD_callingPartyNumber= p_no
ORDER BY FLD_callingPartyNumber; -- there was a comma which is not required or you
-- missed a column
END get_CDR_rs;
/
Secondly where is get_CDR_rs being called to retrieve the results?
Thirdly why do you need the following? because you are using sys_refcursor
CREATE OR REPLACE PACKAGE types AS
TYPE cursor_type IS REF CURSOR;
END Types;
/
If you would like to see the results of your procedure which returns sys_refcursor, do as follows
variable rset refcursor;
DECLARE
p_no zkv.FLD_callingPartyNumber%TYPE;
BEGIN
p_no := '123';
get_CDR_rs (p_no, :rset);
END;
/
print rset

Related

How to fetch the records from cursor which contains columns from different table?

FIRST PROCEDURE
create or replace PROCEDURE TESTPROCEDURE
(
P_LAF_PK IN NUMBER,
P_RET_VAL OUT SYS_REFCURSOR
) AS
BEGIN
OPEN p_RET_VAL FOR
SELECT LAF.AF_FEE,LAF.AF_FEES_PAYABLE,
LAH.LH_DECISION_DT,LAH.LH_ISSUED
FROM LH_APP_HDR LAH JOIN LIQ_APP_FEE LAF
ON AF_PK = LH_PK
WHERE LAF_APPID = P_LAF_PK;
END TESTPROCEDURE;
Calling this procedure in another procedure
SECOND PROCEDURE
create or replace PROCEDURE TESTPROCEDURE1
AS
BEGIN
DECLARE
V_LIQ_CURSOR SYS_REFCURSOR;
V_LIQ_CURSOR_OUT1 LIQ_APP_FEE%ROWTYPE;
BEGIN
TESTPROCEDURE (2727,V_LIQ_CURSOR);
LOOP
FETCH V_LIQ_CURSOR INTO V_LIQ_CURSOR_OUT1; -- getting error in this line like "Return
types of Result Set variables or query do not match"
EXIT WHEN V_LIQ_CURSOR%NOTFOUND;
IF(V_LIQ_CURSOR_OUT1.AF_FEE != 0) THEN
SELECT (V_LIQ_CURSOR_OUT1.AF_FEES_PAYABLE + V_LIQ_CURSOR_OUT1.AF_FEE) INTO V_TOTALFEE FROM DUAL;
END IF;
END TESTPROCEDURE1;
Can anyone tell me how to get the result from cursor which contains multiple table columns.
NOTE:
I want only two columns from the first procedure

How to achive PLSQL procedure overloading package

I have a use case where a package has 2 procedure with the same name and argument, one as CHAR type and another is VARCHAR2 type.
How to call tham?
For PL/SQL the package spec and body compiles ok.
But at runtime the 2 procedures are the same and there is no way to distinguish because CHAR type and VARCHAR2 type are of the same datatype family.
For example:
create or replace package pk_over
is
procedure pr_text(p_isbCaracter char);
procedure pr_text(p_isbCaracter varchar2);
End;
create or replace package body pk_over
is
procedure pr_text(p_isbCaracter char)
is
Begin
Dbms_Output.Put_Line('Soy PR_TEXT parĂ¡metro CHAR="'||p_isbCaracter||chr(34));
End pr_text;
--
procedure pr_text(p_isbCaracter varchar2)
is
Begin
Dbms_Output.Put_Line('Soy PR_TEXT parĂ¡metro VARCHAR2="'||p_isbCaracter||chr(34));
End pr_text;
--
End pk_over;
An you call with:
Declare
sbVar2 varchar2(20);
Begin
sbVar2:='Texto 1';
pk_over.pr_text(sbVar2);
End;
The error at runtime is:
PLS-00307: too many declarations of 'PR_TEXT' match this call
So you don't need the 2 procedures, only one.

Can not perform DML Operation inside a query? While trying to fetch data from collection

here is my PLSQL code:
declare
headerStr varchar2(1000):='C1~C2~C3~C5~C6~C7~C8~C9~C10~C11~C12~C16~C17~C18~C19~RN';
mainValStr varchar2(32000):='1327~C010802~9958756666~05:06AM~NO~DISPOSAL~NDL~4~P32~HELLO~U~28-OCT-2017~28-OCT-2017~Reject~C010741~1;1328~C010802~9958756666~06:07AM~MH~DROP~NDL~1~P32~~U~28-OCT-2017~28-OCT-2017~Reject~C010741~2;1329~C010802~9999600785~01:08AM~BV~DROP~NDL~2~P32~MDFG~U~28-OCT-2017~28-OCT-2017~Reject~C010741~3';
valStr varchar2(4000);
headerCur sys_refcursor;
mainValCur sys_refcursor;
valCur sys_refcursor;
header varchar2(1000);
val varchar2(1000);
iterator number:=1000;
strIdx number;
strLen number;
idx number;
TYPE T_APPROVAL_RECORD IS TABLE OF VARCHAR2(4000) INDEX BY VARCHAR2(1000);
headerTable T_APPROVAL_RECORD;
cnt number;
begin
open headerCur for select * from table(split_str(headerStr,'~'));
open mainValCur for select * from table(split_str(mainValStr,';'));
loop
fetch mainValCur into valStr;
exit when mainValCur%notfound;
insert into header_test values(cnt, valStr); -- for testing purpose
open valCur for select * from table(split_str(valStr,'~'));
loop
fetch valCur into val;
fetch headerCur into header;
exit when valCur%notfound;
exit when headerCur%notfound;
insert into header_test values(header, val);
headerTable(header):= val;
end loop;
idx := headerTable.FIRST; -- Get first element of array
WHILE idx IS NOT NULL LOOP
insert into header_test values (idx, headerTable(idx));
idx := headerTable.NEXT(idx); -- Get next element of array
END LOOP;
headerTable.delete;
end loop;
commit;
end;
c1 c2 ..... c19 are column name and RN is rownumber,
data for the columns of each will be in mainValString seperated by ;
Why i am getting ORA-14551 when i am trying to access collection "headerTable"?
Please help.
Problem is with this line.
idx := headerTable.FIRST;
The index of headertable is of TYPE VARCHAR2 whereas idx is defined as NUMBER.
declare idx as VARCHAR2(1000), it should work.
Having said that, ORA-14551 - Cannot perform DML ... is not related to this error. It is unclear to me why should you encounter this error.
Oh but it does:
EXCEPTION WHEN OTHERS THEN
v_msg:=sqlcode||sqlerrm;
insert into err_data_transfer values('SPLIT_STR',v_msg,sysdate,null);
It may only be during an exception, but it's still DML during a select statement. You may be able to create another procedure as an AUTONOMOUS_TRANSACTION to create the error log. Also, you should either re-raise or raise_application_error afterward. If not your procedure will continue as though the error did not occur; which leads to more problems as to why your main process does not work (including running to completion but doing the wrong thing).

Select a variable to output from within stored procedure in Teradata

I have a stored procedure I've started coding and need to return a value. In SQL Server I could just do a SELECT of the variable to return it. However this does not seem to work with Teradata and have not found a similar example on how to do this. Here is my stored procedure:
REPLACE PROCEDURE sp_Get_MyValue()
BEGIN
DECLARE mytestvar VARCHAR(40);
SELECT mycolumn INTO mytestvar FROM MyTable;
SELECT mytestvar;
END;
I get this error:
STATEMENT 2: REPLACE failed. Failed [5526 : HY000] Stored Procedure
is not created/replaced due to error(s).{Nested Failure Msg [5526 :
HY000] SPL1045:E(L10), Invalid or missing INTO clause.}
I also tried adding an OUT variable to the procedure but that did not work either:
REPLACE PROCEDURE sp_Get_MyValue(mytestvarout VARCHAR(40))
BEGIN
DECLARE mytestvar VARCHAR(40);
SELECT mycolumn INTO mytestvar FROM MyTable;
END;
With this error:
Executed as Single statement. Failed [5531 : HY000] Named-list is not
supported for arguments of a procedure. Elapsed time = 00:00:00.079
To return a single row you must define and OUT-variable and assign a value to it:
REPLACE PROCEDURE sp_Get_MyValue(OUT mytestvarout VARCHAR(40))
BEGIN
DECLARE mytestvar VARCHAR(40);
SELECT mycolumn INTO mytestvar FROM MyTable;
SET mytestvarout = mytestvar;
END;
To return a result set you need to define a kind of dummy-cursor (blame Standard SQL :-)
Returning Result Sets from a Stored Procedure

TYPE cause ORA-06502: PL/SQL: numeric or value error

I have the following PL/SQL package:
CREATE OR REPLACE PACKAGE PKG_JCSJ
AS
TYPE record_organ_cant IS RECORD(CANT_CODE VARCHAR2(90),
ORGAN_ID VARCHAR2(90) ,
CANT_NAME VARCHAR2(90),
SUPP_TYPE VARCHAR2(20));
--TYPE array_organ_cant IS TABLE of PKG_JCSJ.record_organ_cant;
TYPE array_organ_cant IS TABLE of pub_organ_cant%ROWTYPE;
function fn_transe_organ_cant return PKG_JCSJ.array_organ_cant PIPELINED;
END PKG_JCSJ;
create or replace package body PKG_JCSJ is
function fn_transe_organ_cant return PKG_JCSJ.array_organ_cant PIPELINED
as
cursor cursor_organ_cant is select * from pub_organ_cant ;
record_o_c pub_organ_cant%rowtype;
record_o_c2 pub_organ_cant%rowtype;
cant_code VARCHAR2(90);
TYPE ref_cursor IS REF CURSOR;
array_column_value ref_cursor;
sp_cant_code VARCHAR2(90);
begin
open cursor_organ_cant;
loop
fetch cursor_organ_cant into record_o_c;
exit when cursor_organ_cant%notfound;
cant_code := record_o_c.cant_code;
if instr(cant_code, ',')>0 then
open array_column_value for select * from table(fn_split(cant_code));
loop
fetch array_column_value into sp_cant_code;
exit when array_column_value%notfound;
--DBMS_OUTPUT.put_line('---' || sp_cant_code);
record_o_c2.CANT_CODE := sp_cant_code;
record_o_c2.ORGAN_ID := record_o_c.ORGAN_ID;
record_o_c2.CANT_NAME := record_o_c.CANT_NAME;
record_o_c2.SUPP_TYPE := record_o_c.SUPP_TYPE;
--DBMS_OUTPUT.put_line('++++++' || record_o_c2.CANT_CODE);
PIPE ROW (record_o_c2);
end loop;
close array_column_value;
else
PIPE ROW (record_o_c);
end if;
end loop;
close cursor_organ_cant;
return;
end fn_transe_organ_cant;
begin
null;
end PKG_JCSJ;
Why is this statement failing?
TYPE array_organ_cant IS TABLE of PKG_JCSJ.record_organ_cant;
error info is ORA-06502: PL/SQL: numeric or value error. However, when I use the following statement, success!
TYPE array_organ_cant IS TABLE of pub_organ_cant%ROWTYPE;
record_organ_cant is same structure with TABLE pub_organ_cant, I have no idea why the former fails and the latter is successful, what's the difference?
then, package body as follow,
First of all, in your package body you don't have to use PKG_JCSJ. because its declared within the pacakge and should be accessible to any of its function like
create or replace package body PKG_JCSJ is
function fn_transe_organ_cant return array_organ_cant PIPELINED
....
Next, when you declare again you don't need PKG_JCSJ. like
CREATE OR REPLACE PACKAGE PKG_JCSJ
AS
TYPE record_organ_cant IS RECORD(CANT_CODE VARCHAR2(90),
ORGAN_ID VARCHAR2(90) ,
CANT_NAME VARCHAR2(90),
SUPP_TYPE VARCHAR2(20));
TYPE array_organ_cant IS TABLE of record_organ_cant;
function fn_transe_organ_cant return array_organ_cant PIPELINED;
END PKG_JCSJ;

Resources