Default Value for Collection in Oracle Stored Procedure - oracle

I have three tables: test_main (could be empty on start), test_sec (almost always has records) and binding table test_ref(storess unique id combinations of records from test_main and test_sec).
I need to create a package with procedures that deal with inserions and updates of records in test_main and test_ref. I need some help with it.
Here's the code of a package:
create or replace package TEST_PKG is
type t_num is table of number;
procedure ADD (arg_main_id number
, arg_name varchar2
, arg_sec_ids t_num);
end test_pkg;
create or replace package body test_pkg is
procedure ADD (arg_main_id number
, arg_name varchar2
, arg_sec_ids t_num)
is begin
insert into test_main (id, col_name)
values (arg_main_id, arg_name);
commit;
for i in arg_sec_ids.first .. arg_sec_ids.last loop
insert into test_ref (main_id, sec_id)
values (arg_main_id, arg_sec_ids(i));
end loop;
commit;
end ADD;
end test_pkg;
I plan to call this procedure in Oracle APEX 5.1 PL/SQL Dynamic Action as follows:
declare
selection apex_t_number
-- or "test_pkg.t_num" with values from "apex_string.split_numbers" added through a "for" loop later
begin
selection := apex_string.split_numbers(:P1_SELECT2,':');
-- for example, '1:2:3' string results in a [1,2,3] array
ADD (arg_main_id => :P1_MAIN_ID
, arg_name => :P1_MAIN_NAME
, arg_sec_ids => selection);
end;
But I also would like to be able to call it as ADD (arg_main_id => :P1_MAIN_ID, arg_name => :P1_MAIN_NAME) with a default value for arg_sec_ids.
So, is it possible to define default value for a collection in PL/SQL Procedure, and if so, how to?

You can define arg_sec_ids parameter as of in out style is of type t_num.
create or replace package test_pkg is
type t_num is table of number;
procedure add (
arg_main_id number,
arg_name varchar2,
arg_sec_ids in out t_num
);
end;
and suppose to initialize with arg_sec_ids := t_num(1,1,2,3,5,8); in the program body, and if you extend with an integer with value number of elements in the tuple, you can use the next values for arg_sec_ids such as arg_sec_ids(7),..,arg_sec_ids(10).
create or replace package body test_pkg is
procedure add (
arg_main_id number,
arg_name varchar2,
arg_sec_ids in out t_num
) is
begin
arg_sec_ids := t_num(1,1,2,3,5,8);
arg_sec_ids.extend(10);
insert into test_main (id, col_name)
values (arg_main_id, arg_name);
for i in arg_sec_ids.first .. arg_sec_ids.last loop
insert into test_ref (main_id, sec_id)
values (arg_main_id, arg_sec_ids(i));
end loop;
arg_sec_ids(7) := arg_sec_ids(5)+ arg_sec_ids(6);
dbms_output.put_line( ' arg_sec_ids(7)''s values is : '||arg_sec_ids(7) );
commit;
end add;
end;
If arg_sec_ids.extend(x) is omitted (where x>6 ), it's impossible to use arrays with index more than default length( which's 6 in this case ).

Related

ORACLE stored procedure - Store query result

I have the following stored procedure:
CREATE OR REPLACE PROCEDURE SP
(
query IN VARCHAR2(200),
CURSOR_ OUT SYS_REFCURSOR
)
AS
row_ PROCESSED_DATA_OBJECT;
processed PROCESSED_DATA_TABLE;
BEGIN
.....
END;
with
CREATE TYPE processed_data_obj AS OBJECT(
id INTEGER,
value FLOAT
);
/
CREATE OR REPLACE TYPE processed_data_table AS TABLE OF processed_data_obj;
/
I call the stored procedure passing the query to be executed as input parameter.
The query is something like that:
SELECT A,B FROM TABLE WHERE
where A,B and TABLE are not fixed (defined at runtime during java program execution), so I don't know their values in advance.
How could I fetch/store each returned row in my structure?
processed PROCESSED_DATA_TABLE;
Thanks
This is one way you can process a dynamically generated query into a user defined type. Note that, in order for this to work, the structure of your query (columns) must match the data type structure of your type (attributes) otherwise you're in for trouble.
CREATE TYPE processed_data_obj AS OBJECT(
ID INTEGER,
VALUE FLOAT,
constructor FUNCTION processed_data_obj RETURN self AS result
);
/
CREATE OR REPLACE TYPE BODY processed_data_obj IS
constructor FUNCTION processed_data_obj RETURN self AS result IS
BEGIN
RETURN;
END;
END;
/
CREATE OR REPLACE TYPE processed_data_table AS TABLE OF processed_data_obj;
/
CREATE OR REPLACE PROCEDURE sp (
p_query IN VARCHAR2
) AS
cursor_ sys_refcursor;
processed processed_data_table := processed_data_table();
BEGIN
OPEN cursor_ FOR p_query;
loop
processed.EXTEND;
processed(processed.count) := processed_data_obj();
fetch cursor_ INTO processed(processed.count).ID, processed(processed.count).VALUE;
exit WHEN cursor_%notfound;
dbms_output.put_line(processed(processed.count).ID||' '||processed(processed.count).VALUE);-- at this point do as you please with your data.
END loop;
CLOSE cursor_; -- always close cursor ;)
processed.TRIM; -- or processed.DELETE(processed.count);
END sp;
I noticed that, originally, you did put CURSOR_ as an output parameter in your stored procedure, if that is still your goal, you can create your procedure as:
CREATE OR REPLACE PROCEDURE sp (
p_query IN VARCHAR2,
cursor_ out sys_refcursor
) AS
processed processed_data_table := processed_data_table();
BEGIN
OPEN cursor_ FOR p_query;
loop
processed.EXTEND;
processed(processed.count) := processed_data_obj();
fetch cursor_ INTO processed(processed.count).ID, processed(processed.count).VALUE;
exit WHEN cursor_%notfound;
dbms_output.put_line(processed(processed.count).ID||' '||processed(processed.count).VALUE);-- at this point do as you please with your data.
END loop;
-- cursor remains open
processed.TRIM; -- or processed.DELETE(processed.count);
END sp;
In this case just be conscious about handling your cursor properly and always close it when you're done with it.

How to append data to an output parameter which is an array?

I have a type like this ...
CREATE OR REPLACE TYPE TYPE_X
AS
TABLE OF VARCHAR2(4000);
... which is used in a package:
CREATE OR REPLACE PACKAGE PACKAGE_TEST
AS
TYPE DETAILS
IS
RECORD
(
EMPNO NUMBER,
ENAME VARCHAR2(4000),
DEPTNO NUMBER );
TYPE DETAILS_ARRAY
IS
TABLE OF DETAILS;
PROCEDURE PROC_TESTING_2(
X TYPE_X,
Y OUT DETAILS_ARRAY );
END;
CREATE OR REPLACE PACKAGE BODY PACKAGE_TEST
AS
PROCEDURE PROC_TESTING_2(
X TYPE_X,
Y OUT DETAILS_ARRAY )
AS
BEGIN
FOR I IN 1..X.COUNT
LOOP
DBMS_OUTPUT.PUT_LINE(X(I));
SELECT EMPNO,ENAME,DEPTNO INTO Y FROM EMP WHERE DEPTNO=X(I);
END LOOP;
END;
END;
I'd like to print the data into the record type by executing all the values from the TYPE_X list. The data needs to be appended till the loop exits. Appreciate your help. Thanks!!
The approach which requires the least change to your existing code would be to populate a local variable of the record type, then append that to the OUT array.
CREATE OR REPLACE PACKAGE BODY PACKAGE_TEST
AS
PROCEDURE PROC_TESTING_2(
X TYPE_X,
Y OUT DETAILS_ARRAY )
AS
l_rec DETAILS;
BEGIN
-- initialize output array
y := package_test.details_array();
FOR I IN 1..X.COUNT
LOOP
DBMS_OUTPUT.PUT_LINE(X(I));
SELECT EMPNO,ENAME,DEPTNO INTO lrec
FROM EMP
WHERE DEPTNO=X(I);
y.extend();
y(y.count) := lrec;
END LOOP;
END;
END;
This would be inefficient for a large number of rows. In that case you should look to use BULK COLLECT instead:
CREATE OR REPLACE PACKAGE BODY PACKAGE_TEST
AS
PROCEDURE PROC_TESTING_2(
X TYPE_X,
Y OUT DETAILS_ARRAY )
AS
l_rec DETAILS;
BEGIN
SELECT EMPNO,ENAME,DEPTNO
bulk collect INTO y
FROM EMP
WHERE DEPTNO in ( select * from table (X));
END;
END;

PL-SQL delete value from variable

In a PL SQL procedure I've a variable(declare as NUMBER) as follows,
Store_Values
I'm loading some data into this variable by select query.So if this data matches to the value(some value which will be compared against variable in if statement),then how to delete matched value from variable.
below is the procedure:
CREATE OR REPLACE PROCEDURE UPDATE_SIG_NAME_MAPS(SignatureID NUMBER, RuleCateory VARCHAR2, SignatureMessage VARCHAR2, IsDelete NUMBER, IsNew NUMBER) AS
NumberOfValues NUMBER ;
BEGIN
select VALUE BULK COLLECT into NumberOfValues from list_details where LIST_CONFIG_ID in(fetching from other table's);
if NumberOfValues = SignatureID then
**delete from sometable with where clause**
end if;
insert variable remaining values to the table using for loop
END ;
/
As #Aleksej said, you are trying to fetch into a scalar variable using bulk collect so you need to declare your varible as below:
You can follow the instructions and modify your code accordingly.
CREATE OR REPLACE PROCEDURE UPDATE_SIG_NAME_MAPS (
SignatureID NUMBER,
RuleCateory VARCHAR2,
SignatureMessage VARCHAR2,
IsDelete NUMBER,
IsNew NUMBER)
AS
Type NumberOfValue is table of NUMBER;
NumberOfValues NumberOfValue;
BEGIN
SELECT VALUE
BULK COLLECT INTO NumberOfValues
FROM list_details
WHERE LIST_CONFIG_ID IN (fetching from other table's);
For rec in 1..NumberOfValues.count
loop
if NumberOfValues(rec) = SignatureID then
**delete from sometable with where clause**
insert variable remaining values to the table
end loop;
END ;

Table variable as in parameter to populate a table in oracle Stored procedure

Mostly I avoid table variables as input parameters for a stored procedure. Because I do not know how to handle them, but in this case I have no other option. I have a requirement where hundreds of records will be passed on to database from Oracle Agile PLM. What I have to do is to populate a table from the input records/list. For accomplishing this I have developed an object type and then a table type out of that object type.
CREATE OR REPLACE TYPE TEST_USER.MD_TYPE AS OBJECT
(QUERY_REF VARCHAR2 (1000 BYTE),
COL_NAME VARCHAR2 (100 BYTE),
COL_LENGTH VARCHAR2 (50 BYTE),
COL_SEQ NUMBER)
/
CREATE OR REPLACE TYPE TEST_USER.MD_TYPE_TABLE AS TABLE OF MD_TYPE
/
Stored Procedure:
CREATE OR REPLACE PROCEDURE SP_TEST2
(
P_MD_TABLE IN MD_TYPE_TABLE,
p_success OUT number
)
IS
BEGIN
INSERT INTO MDATA_TABLE
(
QUERY_REF ,
COL_NAME ,
COL_LENGTH ,
COL_SEQ
)
SELECT ea.*
FROM TABLE(P_MD_TABLE) ea;
p_success :=1;
EXCEPTION
WHEN OTHERS THEN
p_success := -1;
END SP_TEST2;
The problem is I do not know how to populate, first parameter P_MD_TABLE and then MDATA_TABLE. And the procedure compiles without any errors. I have not tested this procedure.
Any help please.
Procedure for loading MD_TYPE_TABLE by passing parameters to MD_TYPE
CREATE OR REPLACE PROCEDURE SP_UPLOAD_MD_TYPE
(
P_QUERY_REF VARCHAR2,
P_COL_NAME VARCHAR2,
P_COL_LENGTH VARCHAR2,
p_col_seq NUMBER,
p_no_of_rows_to_insert NUMBER,
p_num OUT NUMBER
)
IS
p_type_tbl MD_TYPE_TABLE := MD_TYPE_TABLE(); --initialize
BEGIN
<<vartype>>
FOR i IN 1..p_no_of_rows_to_insert
LOOP
p_type_tbl.extend();
p_type_tbl(p_type_tbl.last) := MD_TYPE(P_QUERY_REF, P_COL_NAME, P_COL_LENGTH, p_col_seq);
END LOOP vartype;
SP_TEST2(p_type_tbl, p_num);
END;
You can populate a table type by using extend/ bulk collect
using extend
p_type_tbl.extend();
p_type_tbl(p_type_tbl.last) := MD_TYPE('QUERY_REF1', 'COL_NAME1', 'COL_LENGTH1', 1);
or using bulk collect
SELECT MD_TYPE(c1, c2... cn)
BULK COLLECT INTO p_type_tbl
FROM some_table;
Demo
DECLARE
p_type_tbl MD_TYPE_TABLE := MD_TYPE_TABLE(); --initialize
p_num NUMBER;
BEGIN
p_type_tbl.extend();
p_type_tbl(p_type_tbl.last) := MD_TYPE('QUERY_REF1', 'COL_NAME1', 'COL_LENGTH1', 1);
p_type_tbl.extend();
p_type_tbl(p_type_tbl.last) := MD_TYPE('QUERY_REF2', 'COL_NAME2', 'COL_LENGTH2', 2);
SP_TEST2(p_type_tbl, p_num);
DBMS_OUTPUT.PUT_LINE(p_num);
END;
/
OutPut
1
SELECT * FROM MDATA_TABLE;
OutPut
QUERY_REF COL_NAME COL_LENGTH COL_SEQ
QUERY_REF1 COL_NAME1 COL_LENGTH1 1
QUERY_REF2 COL_NAME2 COL_LENGTH2 2

Oracle PL/SQL return array of rows

Dear Oracle Developers,
I have searched and googled to find a solution for my problem but nothing helped me.
Situation :
TABLE : CUSTOMER(....);
My problem is : I want to create a stored procedure say get_customers to return an array of customer rows. I have no idea how to get this working.
I tried to create a type customer_rec and using a cursor to retrieve no more than maxRows
create or replace procedure get_customers(maxRows IN NUMBER, ??? OUT ????)
How to define the OUT parameter ?
How to retrieve the rows in the array using a cursor ?
Thanks a lot
I would like to answer this in a way that discourages passing around arrays, when passing around cursors is a more sound approach. It doesn't exactly answer the question as posed, but it is an answer. Thinking cursors instead of thinking arrays is more efficient and thus more scalable. Also, it can be much easier code to maintain.
create table customer (
customer_id number(2) primary key,
customer_name varchar2(200) );
insert into customer values (1, 'Customer One');
insert into customer values (2, 'Customer Two');
insert into customer values (3, 'Customer Three');
insert into customer values (4, 'Customer Four');
insert into customer values (5, 'Customer Five');
insert into customer values (6, 'Customer Six');
insert into customer values (7, 'Customer Seven');
CREATE OR REPLACE PACKAGE cursor_not_array IS
FUNCTION get_customers(p_max_records INTEGER, p_id_start INTEGER, p_id_end INTEGER DEFAULT NULL) RETURN SYS_REFCURSOR;
END cursor_not_array;
CREATE OR REPLACE PACKAGE BODY cursor_not_array IS
c_max_customer_id CONSTANT NUMBER(2) := 99;
FUNCTION get_customers(p_max_records INTEGER, p_id_start INTEGER, p_id_end INTEGER DEFAULT NULL) RETURN SYS_REFCURSOR IS
v_result SYS_REFCURSOR;
BEGIN
OPEN v_result FOR
SELECT customer_id,
customer_name
FROM customer
WHERE customer_id BETWEEN p_id_start AND nvl(p_id_end, c_max_customer_id)
ORDER BY customer_id;
RETURN v_result;
END;
END cursor_not_array;
You could create a package like this:
create or replace package customers is
type customers_array is table of customer%rowtype index by binary_integer;
procedure get_customers(maxRows IN NUMBER, customer_array OUT customers_array);
end customers;
create or replace package body customers is
procedure get_customers(maxRows IN NUMBER, customer_array OUT customers_array) is
cursor c_customers is
select *
from customers;
where rownum <= maxRows;
i number := 1;
begin
for r in c_customers loop
customer_array(i) := r;
i := i + 1;
end loop;
end get_customers;
end customers;
And then call the get_customers procedure from wherever you want to...
first time create VARRAY type.
'create TYPE CUSTARRAY is VARRAY(100) OF VARCHAR2(30);'
varray limit is depends on you.
then create procedure that return CUSTARRAY type parameter.
`create
procedure prc_get_arr(p_maxrow in number, p_customers out custarray)
as
my_cust custarray := custarray();
cursor c_cust is select name from CUSTOMER where rownum<p_maxrow;
v_customer varchar2(64);
begin
open c_cust;
loop
fetch c_cust into v_customer;
exit when c_cust%notfound;
my_cust.extend;
my_cust(my_cust.count) := v_customer;
end loop;
close c_cust;
p_customers:=my_cust;
end;`
Now call this procedure
DECLARE
P_MAXROW NUMBER;
p_customers custarray;
v_cnt number:=0;
begin
P_MAXROW := 22;
prc_get_arr( p_maxrow => p_maxrow, p_customers => p_customers );
v_cnt:=p_customers.count;
for i in p_customers.first..p_customers.last loop
dbms_output.put_line('P_CUSTOMERS = ' || p_customers(i));
end loop;
end;
You can, using the SYS_REFCURSOR on your function output.
First you have to define a collection :
TYPE customers_array IS TABLE OF customer%ROWTYPE
INDEX BY BINARY_INTEGER;
Then your procedure simply have to fetch the result into that collection.
You're procedure could be written as follow:
CREATE OR REPLACE PACKAGE your_pkg
AS
TYPE customers_array IS TABLE OF customer%ROWTYPE
INDEX BY BINARY_INTEGER;
PROCEDURE get_customers(pn_max_rows IN NUMBER,
pt_coustomers OUT customers_array);
END your_pkg;
CREATE OR REPLACE PACKAGE BODY your_pkg
AS
PROCEDURE get_customers(pn_max_rows IN NUMBER,
pt_coustomers OUT customers_array)
IS
BEGIN
SELECT *
BULK COLLECT INTO pt_coustomers
FROM customers
WHERE rownum <= pn_max_rows;
END get_customers;
END your_pkg;

Resources