wrong number or types of arguments in call to P_AA - oracle

When I try to compile the procedure with collection type as in parameter, I'm getting the error like
wrong number or types of arguments in call to 'P_AA'
-------Procedure created with in parameter as nested table------------
create or replace procedure p_aa(serv in t45)
is
aa serv_item%rowtype;
begin
for i in 1..serv.count
loop
select a.* into aa from serv_item a where a.serv_item_id = serv(i);
dbms_output.put_line('Serv item '||aa.serv_item_id||' '||'status '||aa.status);
end loop;
end;
/
----------Calling the package----------
declare
type t1 is table of number;
cursor c1 is select serv_item_id from serv_item;
n number:=0;
v t1;
begin
open c1;
loop
fetch c1 into v(n);
exit when c1%notfound;
n:=n+1;
end loop;
close c1;
p_aa(v);
end;
/
How can I fix my code?

Your procedure defines the parameter like this:
serv in t45
So t45 is the defined datatype of the parameter.
Now when you call the procedure you pass in a variable v. And how is v defined?
type t1 is table of number;
...
v t1;
t1 is a different type to t45. Even if they have identical structures they are different types. And that's why you get PLS-00306. The solution is quite simple: define v as t45.
i am getting error like 'Reference to uninitialized collection'.
You need to initialise the collection. You do this using the default constructor of the type, either at the start of the program ...
v := t45();
... or when you declare it:
v t45 := t45();
Once you get beyond that you will find your assignment logic is wrong: you're fetching into an element of the collection before you increment the counter or extend the array. So what you need is this:
declare
cursor c1 is select serv_item_id from serv_item;
n number:=0;
v t45 := t45();
x number;
begin
open c1;
loop
fetch c1 into x;
exit when c1%notfound;
n:=n+1;
v.extend();
v(n) := x;
end loop;
close c1;
p_aa(v);
end;
/
Alternatively use the less verbose bulk collect, which handles all the looping and type management implicitly :
declare
v t45;
begin
select serv_item_id
bulk collect into v
from serv_item;
p_aa(v);
end;
/
Here is a db<>fiddle demo showing both approaches working.

Related

How to pass array as input parameter in Oracle Function

I have a function to BULK insert data using FORALL.
create or replace type l_array_tab as table of number;
create or replace FUNCTION fn_insert_using_array(
L_TAB VARCHAR2,
L_COL_NAME VARCHAR2,
L_ARRAY L_ARRAY_TAB)
RETURN NUMBER
AS
SQL_STMT VARCHAR2(32767);
sql_count NUMBER;
BEGIN
FORALL i IN L_ARRAY.first .. L_ARRAY.LAST
EXECUTE immediate 'INSERT INTO my_table
Select * from '||L_TAB
||' where '||L_COL_NAME||' := :1' using L_ARRAY(i);
sql_count:= SQL%ROWCOUNT;
RETURN SQL_COUNT;
end;
I need to call this function from another stored procedure or plsql block in this example. While calling this function, I am getting error as wrong number or type of inputs.
This is how I am calling the function:
create or replace type l_array_orig_tab as table of number;
Declare
l_array_orig l_array_orig_tab :=l_array_orig_tab();
l_tab varchar2(30): ='my_tab_orig';
l_col_name varchar2(30) :='insert_id';
V_COUNT NUMBER;
cursor c1 is select * from my_tab_orig;
begin
open c1;
LOOP
FETCH c1 BULK COLLECT INTO l_array_orig limit 1000;
EXIT WHEN L_ARRAY_orig.COUNT =0;
V_COUNT:= fn_insert_using_array(L_TAB, L_COL_NAME,l_array_orig);
END LOOP;
END ;
Please suggest how to call the function.
I am getting error as wrong number or type of inputs
You are getting the error because l_array_orig_tab is a different type from l_array_tab. It doesn't matter that they have the same structure, as far as Oracle knows they are different types. Oracle is a database engine and it strongly enforces type safety. There is no duck typing here.
So the simplest solution is to use the correct type when calling the function:
Declare
l_array_orig l_array_tab :=l_array_tab(); -- change this declaration
l_tab varchar2(30): ='my_tab_orig';
l_col_name varchar2(30) :='insert_id';
V_COUNT NUMBER;
cursor c1 is select * from my_tab_orig;
begin
open c1;
LOOP
FETCH c1 BULK COLLECT INTO l_array_orig limit 1000;
EXIT WHEN L_ARRAY_orig.COUNT =0;
V_COUNT:= fn_insert_using_array(L_TAB, L_COL_NAME,l_array_orig);
END LOOP;
END ;
"The function fn_insert_using_array is in a different schema and also the Type."
So the schema which owns the function has granted you EXECUTE privilege on the function. But they also need to grant you EXECUTE on the type. This is their responsibility: they defined the function with a UDT in its signature so they have to give you all the privileges necessary to call it.
I don't don't whether this is a toy example just for posting on SO, but if it isn't there is no need to create a type like this. Instead use the documented Oracle built-in table of numbers, sys.odcinumberlist.
Is l_array_orig_tab != l_array_tab
you have to use the same type or do the cast between type.
Declare
l_array_orig l_array_orig_tab;
new_array l_array_tab;
l_tab varchar2(30): ='my_tab_orig';
l_col_name varchar2(30) :='insert_id';
V_COUNT NUMBER;
cursor c1 is select * from my_tab_orig;
begin
open c1;
LOOP
FETCH c1 BULK COLLECT INTO l_array_orig limit 1000;
select cast( l_array_orig as l_array_tab) into new_array from dual;
EXIT WHEN L_ARRAY_orig.COUNT =0;
V_COUNT:= fn_insert_using_array(L_TAB, L_COL_NAME,new_array);
END LOOP;
END ;
How cast works.
select cast( variable as destination_type) into var_destination_type from dual

Oracle PL/SQL dynamic if statement global vars

I'm having trouble with dynamic sql, Issue is (I think) reading and setting global variable. Here's what I have and any help at all is greatly appreciated. Please let me know if you need table data too although I have included the data in comments.
CREATE OR REPLACE PACKAGE data_load
IS
curr_rec NUMBER;
curr_rule VARCHAR2(200);
curr_sql VARCHAR2(4000);
curr_sql_two VARCHAR2(4000);
curr_data_element VARCHAR2 (200);
curr_rule_text VARCHAR2(200);
curr_error_code VARCHAR2(10);
curr_error_flag VARCHAR2(10);
curr_flag_val NUMBER;
v_check NUMBER;
v_ID NUMBER;
cur_hdl INT ;
rows_processed NUMBER;
PROCEDURE check_rules;
END data_load;
The package body:
create or replace PACKAGE BODY data_load IS
PROCEDURE check_rules IS
CURSOR c1
IS
SELECT * FROM STAGING_TABLE where rownum < 3;
CURSOR c2
IS
SELECT * FROM ERROR_CODES WHERE rule_text IS NOT NULL AND status =1;
BEGIN
FOR rec1 IN c1
LOOP
FOR rec2 IN c2
LOOP
curr_data_element := 'rec1.'||rec2.data_element; --- this results in value "rec1.SHIP_FROM_ACCOUNT_ORG_CODE" without quotes
curr_rule_text := rec2.rule_text; --- this value is "is not null" without quotes
curr_error_flag := rec2.error_flag; --this value is "FLAG_03" without quotes
curr_flag_val := to_number(rec2.error_code); --- this value is 31
curr_sql :='begin if :curr_data_element '||curr_rule_text||' then update table_with_column_FLAG_03 set '||curr_error_flag ||' = 0; else update table_with_column_FLAG_03 set '||curr_error_flag ||' = '||curr_flag_val||'; end if; end;';
dbms_output.put_line(curr_sql); -- results in "begin if :curr_data_element is null then update table_with_column_FLAG_03 set FLAG_03 = 0; else update table_with_column_FLAG_03 set FLAG_03 = 31; end if; end;"
EXECUTE IMMEDIATE curr_sql USING curr_data_element ; -- this always updates the column with 31 even when curr_data_element/ rec1.SHIP_FROM_ACCOUNT_ORG_CODE is null and that's the problem
COMMIT;
END LOOP;
curr_rec := curr_rec+1;
END LOOP;
dbms_output.put_line(curr_rec);
END check_rules;
END data_load;
You've already highlighted the problem really:
curr_data_element := 'rec1.'||rec2.data_element; --- this results in value "rec1.SHIP_FROM_ACCOUNT_ORG_CODE" without quotes
You can't refer to cursor columns dynamically. You are creating a string with value 'rec1.SHIP_FROM_ACCOUNT_ORG_CODE'; there is no mechanism to evaluate what that represents. You can't, for instance, try to dynamically select that from dual because the rec1 is not in scope for a SQL call, even dynamically.
When you bind that string value it is never going to be null. You are using that string, not the value in the outer cursor that it represents, and essentially you cannot do that.
The simplest way to deal with this, if you have a reasonably small number of columns in your staging table that might appear as the rec2.data_element value, is to use a case expression to assign the appropriate actual rec1 column value to the curr_data_element variable, based on the rec2.data_element value:
...
BEGIN
FOR rec1 IN c1
LOOP
FOR rec2 IN c2
LOOP
curr_data_element :=
case rec2.data_element
when 'SHIP_FROM_ACCOUNT_ORG_CODE' then rec1.SHIP_FROM_ACCOUNT_ORG_CODE
when 'ANOTHER_COLUMN' then rec1.ANOTHER_COLUMN
-- when ... -- repeat for all possible columns
end;
curr_rule_text := rec2.rule_text;
...
If you have a lot of columns you could potentially do that via a collection but it may not be worth the extra effort.
The curr_sql string stays the same, all that's changing is that you're binding the actual value from the relevant rec1 column, rather than never-null string you were forming.

PLS-00221: 'C1'(cursor) is not a procedure or is undefined

I am creating a package to use with Jasper reports where I learnt that I need SYS_REFCURSOR but I cannot seem to be able to Loop my cursors:eg
create or replace PACKAGE BODY fin_statement_spool
AS
PROCEDURE fin_main_spool(vacid in VARCHAR2, vfromdate in date, vtodate in date,c1 out SYS_REFCURSOR,c2 out SYS_REFCURSOR)
AS
cramount NUMBER;
dramount NUMBER;
countcr NUMBER;
countdr NUMBER;
BEGIN
OPEN c1 FOR
SELECT
.......;
OPEN c2 FOR
SELECT ........;
BEGIN
FOR i IN c1--Error is here
LOOP
rnum := 0;
cramount := 0;
dramount := 0;
countdr := 0;
countcr := 0;
..........
Isn't this the right way?
You appear to have confused explicit cursors, e.g.:
declare
cursor cur is
select dummy from dual;
begin
for rec in cur
loop
dbms_output.put_line(rec.dummy);
end loop;
end;
/
with a ref cursor - which is a pointer to an open cursor.
You would typically use a ref cursor to open a cursor in the db and pass it back to the calling app for it to loop through.
The way you have declared the ref cursors as out parameters and then tried to loop through them in the same procedure does not make sense - once you have fetched a record from a cursor, you cannot re-fetch it.
If you absolutely must loop through a ref cursor, you'd use this sort of syntax:
declare
cur sys_refcursor;
rec dual%rowtype;
begin
open cur for select dummy from dual;
loop
fetch cur into rec;
exit when cur%notfound;
dbms_output.put_line(rec.dummy);
end loop;
end;
/
but as I said, in general, you wouldn't be looping through ref cursors in the db, you'd be doing that in the calling code.
Perhaps if you updated your question with the requirements you're trying to fulfil, we could suggest a better way of doing it.

oracle weakly typed record as parameter

Let's say I have this construct in a PLSQL procedure:
...
for rec in
(
select a, b, c from t;
)
loop
process_record(rec);
end loop;
...
procedure process_record(p_rec in ???)
...
How do I pass rec, which is a record of a weakly typed cursor, to a procedure for processing.
I don't want to define a cursor and a type for the record of this type.
Is this possible?
TIA
Gold
you have to define a cursor, or a type (both not needed) in order to tell oracle the structure of the input (unless you want to go down the route or defining an AnyData/AnyType approach.
eg
declare
cursor my_template
is
select a,b,c from t;
procedure process_record(p_rec in my_template%rowtype)
is
begin
null;
end;
begin
for rec in
(
select a, b, c from t
)
loop
process_record(rec);
end loop;
end;
/
I don't know of a simple way to to that (and would be most surprised to see one) but I have a workaround:
Use XMLType as your type:
declare
begin
for rec in (select xmlelement("p_rec", xmlforest(a, b, c)) r from t) loop
process_record(rec.r);
end loop;
end;
...
create or replace procedure process_record(p_rec in XMLtype) as
BEGIN
dbms_output.put(p_rec.extract('//A/text()').getstringval() || ',');
dbms_output.put(p_rec.extract('//B/text()').getstringval() || ',');
dbms_output.put_line(p_rec.extract('//C/text()').getstringval());
END;
BTW, why do you want to do that?

An elegant way to transform an index by table into simple table in PL/SQL, other then loop through every entry

I have two types
CREATE OR REPLACE TYPE my_record_type IS OBJECT
(
name varchar2(30)
)
;
CREATE OR REPLACE TYPE my_table_type AS TABLE OF my_record_type
and a function
create or replace my_function return my_table_type
is
type my_hash_type is table of my_record_type index by pls_integer;
v_hash my_hash_type;
v_table my_table_type;
i NUMBER;
begin
-- some business logic here
-- transformation part
v_table := my_table_type();
i := v_hash.first;
while i is not null loop
v_table.extend(1);
v_table(v_table.last) := v_hash(i);
i := v_hash.next(i);
end loop;
-- end transformation part
return v_table;
end;
/
Is there an elegant way in 10g to replace the transformation part with something like
v_table = CAST( v_hash as my_table_type )
You may use the SELECT my_record_type(column_value) BULK COLLECT INTO v_table from table(v_hash). But in order to use this, you will have to create my_hash_type outside of a function (either as a stand along type OR in a Package Specification so it will be visible to the SQL Engine) otherwise you will receive a PLS-00642: local collection types not allowed in SQL statements.
CREATE OR REPLACE TYPE my_hash_type is table OF VARCHAR2(10);
/
set serveroutput on
declare
--type my_hash_type is table OF VARCHAR2(10);
v_hash my_hash_type := my_hash_type();
v_table my_table_type;
i NUMBER;
begin
null ;
for n in 60..75 loop
V_hash.extend(1);
V_hash(v_hash.count) := chr(n) ;
end loop ;
select my_record_type(column_value)
bulk collect into v_table
from table(v_hash) ;
for n in 1..v_table.count loop
dbms_output.put_line( n || ':>' || v_table(n).name);
end loop ;
--PLS-00642: local collection types not allowed in SQL statements
end ;
1:><
2:>=
3:>>
4:>?
5:>#
6:>A
7:>B
8:>C
9:>D
10:>E
11:>F
12:>G
13:>H
14:>I
15:>J
16:>K
have a look here and here for some more examples and whatnot
timing differences (based on this methodology #s are in hundreths of a second)
pl/sql context switch (as described above)
44
42
43
42
loop fill (with type defined outside of block) --A distinct CREATE TYPE on Oracle level
18
18
18
18
loop fill (with type defined within block) --Type created within the Anon. block
23
22
24
22
(the above time trials were variations based on this code:
set serveroutput on
declare
--type my_hash_type is table of my_record_type -index by pls_integer;
v_hash my_hash_type := my_hash_type();
v_table my_table_type;
i NUMBER;
time_before BINARY_INTEGER;
time_after BINARY_INTEGER;
begin
time_before := DBMS_UTILITY.GET_TIME;
for n in 0..15000 loop
V_hash.extend(1);
V_hash(v_hash.count) := my_record_type(n) ;
end loop ;
select my_record_type(column_value)
bulk collect into v_table
from table(v_hash) ;
/*
v_table := my_table_type();
for n in 1..V_hash.count loop
v_table.extend(1);
v_table(v_table.count) := v_hash(n) ;
--dbms_output.put_line( n || ':>' || v_table(n).name);
end loop ;*/
--for n in 1..v_table.count loop
-- dbms_output.put_line( n || ':>' || v_table(n).name);
--end loop ;
time_after := DBMS_UTILITY.GET_TIME;
DBMS_OUTPUT.PUT_LINE (time_after - time_before);
--PLS-00642: local collection types not allowed in SQL statements
end ;
/
Thus the loop fill is 50% faster, but the time difference is still minuscule (here is the balance between premature optimization and avoiding something because it may be too long, I would recommend doing time trials on your real data to find the solution that best fits).
The only other 'elegant' solution I can think of is TREAT, but you'll notice it requires a subtype/supertype solution that must be on an object type (I couldn't get it to work on an Varray/Assoc. Array type -- hopefully I'm wrong!)

Resources