Pipelined function with a parameter declare with %ROWTYPE - oracle

I'm trying to declare a pipelined table function (t) inside a package that takes an argument declared as <tablename>%ROWTYPE. Declaring that function works and the package compiles without any error.
But I would like to use this function inside a procedure (p1) like shown below.
CREATE OR REPLACE PACKAGE BODY t1
AS
-- private
PROCEDURE p1
IS
l_person persons%ROWTYPE;
BEGIN
FOR l_row IN (SELECT *
FROM TABLE (t (l_person)))
LOOP
NULL;
END LOOP;
END;
-- public
FUNCTION t (p_persons_record persons%ROWTYPE)
RETURN t_a_list
PIPELINED
IS
l_a t_a;
BEGIN
l_a.dummy := 'A';
PIPE ROW (l_a);
END;
END;
This sample code does not makes sense but it demonstrates my problem.
It just doesn't compile but gives the following errors:
[Error] PLS-00382 (10: 38): PLS-00382: expression is of wrong type
[Error] PLS-00306 (10: 35): PLS-00306: wrong number or types of arguments in call to 'T'
[Error] ORA-00904 (10: 35): PL/SQL: ORA-00904: "T1"."T": invalid identifier
Can anyone explain what's wrong and how to fix those errors?
Edit:
The package spec is:
CREATE OR REPLACE PACKAGE t1
AS
TYPE t_a IS RECORD (dummy VARCHAR2 (1));
TYPE t_a_list IS TABLE OF t_a;
FUNCTION t (p_persons_record persons%ROWTYPE)
RETURN t_a_list
PIPELINED;
END;

You cannot use a record type in SQL Scope. So a PL/SQL function with a record parameter derived by rowtype attribute cannot be used as table function in SQL.
The only thing you have to rewrite is to use a SQL object on schema level instead of a PL/SQL record.
-- adapt your columns to your table as necessary
CREATE OR REPLACE TYPE g_persons AS OBJECT (
ID int,
C1 int
);
CREATE OR REPLACE PACKAGE t1
AS
TYPE t_a IS RECORD (dummy VARCHAR2 (1));
TYPE t_a_list IS TABLE OF t_a;
FUNCTION t (p_persons_record g_persons_t)
RETURN t_a_list
PIPELINED;
END;
/
CREATE OR REPLACE PACKAGE BODY t1
AS
-- private
PROCEDURE p1
IS
l_person g_persons_t;
BEGIN
l_person.ID := 1; -- init your record some how
l_person.C1 := 1; -- init your record some how
FOR l_row IN (SELECT *
FROM TABLE (t (l_person)))
LOOP
NULL;
END LOOP;
END;
-- public
FUNCTION t (p_persons_record g_persons_t)
RETURN t_a_list
PIPELINED
IS
l_a t_a;
BEGIN
l_a.dummy := 'A';
PIPE ROW (l_a);
END;
END;
/

I think your PACKAGE specification lacks t's declaration. Have you checked that your PACKAGE specification is something similar to this?:
CREATE OR REPLACE PACKAGE t1 AS
PROCEDURE p1;
FUNCTION t (p_persons_record persons%ROWTYPE) RETURN t_a_list PIPELINED;
END;

Related

Pipelining Between PL/SQL Table Functions

I have a package with 2 pipelined functions. When I'm trying to call one function with another function as it's argument I'm getting "ORA-06553: PLS-306: wrong number or types of arguments in call" error.
Here is the package:
create or replace NONEDITIONABLE TYPE RESULTING_RECORD_RT as object
(
CALENDAR NVARCHAR2(1024),
PRODUCT NVARCHAR2(1024),
MEASURE NVARCHAR2(1024),
VALUE NUMBER
);
/
create or replace NONEDITIONABLE TYPE RESULTING_COLS_RT IS TABLE OF RESULTING_RECORD_RT;
/
create or replace package pipe_pkg as
function pipe_func_emp return RESULTING_COLS_RT PIPELINED;
function pipe_func_emp2(input_Set IN resulting_cols_rt) return RESULTING_COLS_RT PIPELINED;
end;
/
create or replace package body pipe_pkg as
function pipe_func_emp return RESULTING_COLS_RT
PIPELINED
is
test_tbl resulting_cols_rt:= resulting_cols_rt();
begin
test_tbl.extend;
test_tbl(1):=resulting_record_rt('A','B','C',1);
test_tbl.extend;
test_tbl(2):=resulting_record_rt('A','B','D',2);
PIPE ROW(test_tbl(1));
PIPE ROW(test_tbl(2));
return;
end;
function pipe_func_emp2(input_Set IN resulting_cols_rt) return RESULTING_COLS_RT
PIPELINED
is
v_tmp NVARCHAR2(10240);
l_res SYS_REFCURSOR;
recs resulting_record_rt;
begin
open l_res for select * from table(input_Set);
loop
fetch l_res into recs;
PIPE ROW(recs);
exit when l_res%notfound;
end loop;
close l_res;
return;
end;
end;
/
I'm calling the functions as follows:
select * from TABLE(pipe_pkg.pipe_func_emp2(CURSOR(select * from TABLE(pipe_pkg.pipe_func_emp()))));
And the call throws error:
ORA-06553: PLS-306: wrong number or types of arguments in call to 'PIPE_FUNC_EMP2'
06553. 00000 - "PLS-%s: %s"
What am I doing wrong?
The function pipe_func_emp2 expected RESULTING_COLS_RT as it's argument, but got REF CURSOR. These are incompatible types.
Try following reproducible example of a chaining of the pipelined functions:
create or replace type somerow as object (id int, val varchar2 (8))
/
create or replace type sometab is table of somerow
/
create or replace package pack as
function func1 return sometab pipelined;
function func2 (cur sys_refcursor) return sometab pipelined;
end;
/
create or replace package body pack as
function func1 return sometab pipelined is
tab sometab := sometab (somerow (1,'AAA'), somerow (2,'BBB'));
begin
for i in 1..tab.count loop
pipe row (tab(i));
end loop;
return;
end;
function func2 (cur sys_refcursor) return sometab pipelined is
sr somerow;
begin
loop
fetch cur into sr;
exit when cur%notfound;
pipe row (sr);
end loop;
close cur;
return;
end;
end;
/
The query and it's outcome:
select *
from table (pack.func2 (
cursor (select value (p) from table (pack.func1()) p )))
/
ID VAL
---------- --------
1 AAA
2 BBB

what is wrong passing a record type as parameter

I have the following code snippets:
CREATE OR REPLACE PACKAGE pkg_test1 IS
TYPE t_rec1 is RECORD(c1 number);
FUNCTION f1(p1 t_rec1 ) RETURN VARCHAR2;
END pkg_test1;
CREATE OR REPLACE PACKAGE BODY pkg_test1 IS
FUNCTION f1 (p1 t_rec1) return varchar2 is
BEGIN
RETURN 'a';
END f1;
END pkg_test1;
DECLARE
TYPE t_rec IS RECORD (c1 NUMBER);
v_rec t_rec;
v_var VARCHAR2(15);
BEGIN
v_rec.c1 := 1;
v_var := pkg_test1.f1 (v_rec);
END;
When I execute the anonym code, I received the following error:
PLS-00306: wrong number or types of arguments in call to 'F1'
Can someone help me to see the mistake I make, please?
The type you declare in your anonymous block looks the same to you, but to Oracle it's a completely independent and incompatible type.
From the documentation:
A RECORD type defined in a package specification is incompatible with an identically defined local RECORD type.
You don't need it though; just use the package type:
DECLARE
-- don't create a new type....
--TYPE t_rec IS RECORD (c1 NUMBER);
--v_rec t_rec;
-- use the package type instead
v_rec pkg_test1.t_rec1;
v_var VARCHAR2(15);
BEGIN
v_rec.c1 := 1;
v_var := pkg_test1.f1 (v_rec);
END;
db<>fiddle

PLSQL Getting expression is of wrong type

I'm a newbee to PLSQL and I'm facing issues while created PLSQL function.It says expression is of wrong type.I need some help.
Here is my function
CREATE OR REPLACE TYPE HOTGROUPTYPE AS OBJECT (
IMEI VARCHAR2(255),
LACCI VARCHAR2(255),
FRAUD_TYPE VARCHAR2(255),
MSISDN VARCHAR2(255)
);
/
CREATE OR REPLACE TYPE HOTGROUPTYPE_tab IS TABLE OF HOTGROUPTYPE;
/
CREATE OR REPLACE FUNCTION get_tab_tf (p_count IN NUMBER, p_days IN NUMBER) RETURN HOTGROUPTYPE_tab
AS
l_tab HOTGROUPTYPE_tab:=HOTGROUPTYPE_tab();
BEGIN
for i in (select IMEI,LACCI,FRAUD_TYPE,MSISDN
from fms_fraud_master_tbl
where request_type='BARRING'
and rownum<2)
loop
l_tab.extend;
l_tab(l_tab.last) := i.IMEI;
end loop;
RETURN l_tab;
END;
/
And I'm getting this error when the execute the function
11/3 PL/SQL: Statement ignored
11/28 PLS-00382: expression is of wrong type
You are not assigning the values to your table type properly. l_tab(<index>) must be assigned the variable of type HOTGROUPTYPE.
You must use this:
l_tab(l_tab.last) := HOTGROUPTYPE(i.IMEI, i.LACCI,i.FRAUD_TYPE,i.MSISDN );
The neatest way to populate a collection from a query is to use bulk collect:
CREATE OR REPLACE FUNCTION get_tab_tf (p_count IN NUMBER, p_days IN NUMBER) RETURN HOTGROUPTYPE_tab
AS
l_tab HOTGROUPTYPE_tab:=HOTGROUPTYPE_tab();
BEGIN
select HOTGROUPTYPE( IMEI,LACCI,FRAUD_TYPE,MSISDN )
bulk collect into l_tab
from fms_fraud_master_tbl
where request_type='BARRING' ;
RETURN l_tab;
END;
/

Nested PIPELINED function

create type data_type_1 as object (x number, y number)
/
create type table_type_1 as table of data_type_1
/
create or replace package xyz AS
function main_xyz return table_type_1 pipelined;
function sub_func return table_type_1 pipelined;
function sub_func1 return table_type_1 pipelined;
end xyz;
/
create package body XYZ AS
function main_xyz return data_type_1 pipelined is
begin
--code
--pipe row(sub_func); --edit_1
FOR rec in (select * from table(sub_func1(x,y))) LOOP
pipe row(rec);
END LOOP;
end;
--function sub_func return data_type_1 pipelined is --edit_1
--begin --edit_1
--code --edit_1
--pipe row(def); --def is data_type_1 --edit_1
--end; --edit_1
function sub_func_1(x in number, y in number) return data_type_1 pipelined is
begin
--code
loop
pipe row(abc); --abc is data_type_1
end loop;
end;
end;
create package body ABC AS
function main_ABC is
begin
--code
FOR rec in (select * from table(main_xyz)) LOOP
pipe row(rec);
END LOOP;
end;
end;
Error that I obtain is...
Error is showed in the block of main_xyz where sub_func1 is called.
[Error] PLS-00382 (): PLS-00382: expression is of wrong type
[Error] PLS-00306 (): PLS-00306: wrong number or types of arguments in call to
[Error] ORA-00904 (): PL/SQL: ORA-00904: : invalid identifier
[Error] PLS-00364 (): PLS-00364: loop index variable 'REC' use is invalid
What is wrong in the above code? and why?
Your functions are returning data_type_1, and the table collection is trying to consume that too. But both need a collection type, even if you expect them to only return a single value (in which case there isn't much point pipelining). You can't pipe a collection type directly, you pipe a member of the collection. So data_type_1 should be a scalar or object/record type, and you need another type which is a collection of those.
create type data_type_1 as object (x number, y number)
/
create type table_type_1 as table of data_type_1
/
create or replace package xyz AS
function main_xyz return table_type_1 pipelined;
function sub_func return table_type_1 pipelined;
function sub_func1 return table_type_1 pipelined;
end xyz;
/
create or replace package body xyz as
function main_xyz return table_type_1 pipelined is
begin
--code
for rec in (select * from table(sub_func)) loop
pipe row(data_type_1(rec.x, rec.y));
end loop;
for rec in (select * from table(sub_func1)) loop
pipe row(data_type_1(rec.x, rec.y));
end loop;
end;
function sub_func return table_type_1 pipelined is
def data_type_1;
begin
--code
pipe row(def); --def is data_type_1
end sub_func;
function sub_func1 return table_type_1 pipelined is
abc data_type_1;
begin
--code
loop
pipe row (abc); --abc is data_type_1
end loop;
end sub_func1;
end xyz;
/
So I've added a table type of your existing data_type_1, and changed the function definitions to return that table type instead. The pipe row still uses data_type_1 - each is a row in the table type. Your loop needs a query for its cursor, not a direct call to table(), so I've changed that too. And the pipe row(sub_func); also needs to be a similar loop over a query.
You only tagged this as PL/SQL but because you may intend to call main_xyz from plain SQL, and because you're calling the sub-functions from a SQL context in those loops, data_type_1 and table_type_1 need to be created at schema level rather than in PL/SQL. (This has changed a bit in 12c but not enough to help here).
If you wanted to have them as PL/SQL types, declared in the package specification, then you couldn't call the function from a non-PL/SQL context, and you'd have to replace the loops with a call to the function followed by an iteration over the returned collection.

Function having an in parameter and returning a object type

How can I pass number as in parameter and return a object type in a function
create type my_details as
object(v_ename varchar2(20),v_sal number(10));
create or replace function f_emp(v_empno in number)
return my_details
as
v_det my_details;
begin
select ename,sal
into v_det.v_ename,v_det.v_sal
from emp
where empno=v_empno;
return v_det;
end;
When I try to call the function, I get an error
declare
v_type my_details;
begin
v_type:=f_emp(7839);
end;
/
The error stack
declare
*
ERROR at line 1:
ORA-06530: Reference to uninitialized composite
ORA-06512: at "SCOTT.F_EMP", line 6
ORA-06512: at line 4
Can any one help me pointing out where I have made mistake and how can I improve this?
Your function would either need to initialize the object before referencing it
create or replace function f_emp(v_empno in number)
return my_details
as
v_det my_details := new my_details(null, null);
begin
select ename,sal
into v_det.v_ename,v_det.v_sal
from emp
where empno=v_empno;
return v_det;
end;
or your could call the constructor as part of the select
create or replace function f_emp(v_empno in number)
return my_details
as
v_det my_details;
begin
select my_details(ename,sal)
into v_det
from emp
where empno=v_empno;
return v_det;
end;
Putting the constructor in the select statement would be the approach I would generally suggest.

Resources