Calling Oracle Stored proc with user define type array - oracle

I need to call a stored procedure in oracle. One of the parameters is a user defined type like so:
CREATE OR REPLACE TYPE MY_PK AS OBJECT
(
SOURCE_ID INTEGER,
ACCT_SYSTEM VARCHAR2(255)
)
/
CREATE OR REPLACE TYPE MY_PK_ARR AS TABLE OF MY_PK
/
PROCEDURE get_data(pk_in IN my_pk_arr, my_coursor OUT t_cursor)
IS
...
END
I am trying to call it like so:
var pk my_pk_arr
var my_cursor t_cursor
execute myschema.mypackage.get_data (pk , :my_cursor);
I can't figure out how to create the array. Is this possible or I need to create some sort of table to do it?
Thanks

call it like this if you're calling it via a script. sqlplus doesn't support user defined arrays etc.
declare
v_pk my_pk_arr;
v_cur t_cursor;
begin
v_pk := MY_PK_ARR(MY_PK(1, 'a'), MY_PK(2, 'b')); -- or build in a loop etc.
get_data(v_pk, v_cur);
end;
/

Related

Hi, I'm new to PL/SQL and I want to know how to add a variable type in a package

I want to know how is ROT_TMLN_ARRAY type added to a variable in sql developer. I see that ROT_TMLN_ARRAY is also a type and I want to create something similar with another variable.
create or replace PACKAGE BODY AS PKG TMLN
PROCEDURE SP TMLN SVC (
rotnPrngNb IN VARCHAR2
empiRotnDetails OUT ROT_TMLN ARRAY)
U can add the variable as a Type in both IN or OUT Parameter.
CREATE OR REPLACE PROCEDURE P_TMLN_SVC(I_ID IN NUMBER,
O_RESULT OUT T_TABLE)
AS
BEGIN
SELECT OWNER_TYPE BULK COLLECT INTO O_RESULT FROM OWNERES WHERE OWNERS_ID=I_ID;
END P_TMLN_SVC;
Calling Statement:
DECLARE
T_ARRAY T_TABLE;
BEGIN
P_TMLN_SVC(100,T_ARRAY);
END;

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 can i use oracle object type in stored procedure?

I'm beginner in Oracle, and I declare this object type:
create or replace
TYPE behzadtype AS OBJECT
( /* TODO enter attribute and method declarations here */
SESSIONID Number
)
and I want that object use in my stored procedure:
CREATE OR REPLACE PROCEDURE PROCEDURE1 AS
behzadtype t1;
BEGIN
t1.SESSIONID:=12;
DBMS_OUTPUT.PUT_LINE('THE VALUES OF P_K ARE' || t1.SESSIONID);
END PROCEDURE1;
but when compile up procedure i get this error:
Error(2,14): PLS-00201: identifier 'T1' must be declared
How can I write correct procedure? Thanks for all.
When you declare variables in PL/SQL, you state the name of the variable first, followed by its datatype.
In your example, you have declared a variable of behzadtype as being of type t1, and then tried to use a variable called t1 in your code body. It looks like you wanted to declare a variable t1 as being of type behzadtype.
As well as that, you can't assign a value to the object like that - you have to initialise it as an object first.
I think what you're after is:
create or replace type behzadtype as object
(sessionid number);
/
create or replace procedure procedure1 as
t1 behzadtype;
begin
t1 := behzadtype(12);
dbms_output.put_line('THE VALUES OF P_K ARE' || t1.sessionid);
end procedure1;
/
begin
procedure1;
end;
/
THE VALUES OF P_K ARE12
drop type behzadtype;
drop procedure procedure1;

Passing values from Oracle Object type parameter to PLSQL Table type parameter

How can we pass a parameter that is declared as an Oracle Object type to a Procedure having a parameter as PLSQL Table type?
Eg:
Procedure A(p_obj_emp t_obj_emp)
Procedure B(p_emp_tab t_emp_tab)
Where t_obj_emp = Oracle Object and t_emp_tab is a PLSQL Table of binary_integer
How can we pass a parameter that is declared as an Oracle Object type to a Procedure having a parameter as PLSQL Record type?
Eg:
Procedure C(p_obj_dept t_obj_dept)
Procedure D(p_dept_rec t_dept_rec)
Where t_obj_dept = Oracle Object having 2 fields (deptid, deptname) and t_dept_rec is declared in package specification as t_dept_rec with 2 fields (deptid, deptname).
Kindly help with some example.
Thanks in advance
The following compiles for me and appears to do what you want:
CREATE OR REPLACE TYPE t_obj_emp AS OBJECT (
emp_id INTEGER,
emp_name VARCHAR2(100)
);
/
CREATE OR REPLACE TYPE t_obj_dept AS OBJECT (
dept_id INTEGER,
dept_name VARCHAR2(100)
);
/
CREATE OR REPLACE PACKAGE my_pkg AS
TYPE t_emp_tab IS TABLE OF t_obj_emp INDEX BY BINARY_INTEGER;
TYPE t_dept_rec IS RECORD (
dept_id INTEGER,
dept_name VARCHAR2(100)
);
PROCEDURE A(p_obj_emp t_obj_emp);
PROCEDURE B(p_emp_tab t_emp_tab);
PROCEDURE C(p_obj_dept t_obj_dept);
PROCEDURE D(p_dept_rec t_dept_rec);
END;
/
CREATE OR REPLACE PACKAGE BODY my_pkg AS
PROCEDURE A(p_obj_emp t_obj_emp)
IS
v_emp_tab t_emp_tab;
BEGIN
v_emp_tab(1) := p_obj_emp;
B(v_emp_tab);
END;
PROCEDURE B(p_emp_tab t_emp_tab) IS BEGIN NULL; END;
PROCEDURE C(p_obj_dept t_obj_dept)
IS
v_dept_rec t_dept_rec;
BEGIN
v_dept_rec.dept_id := p_obj_dept.dept_id;
v_dept_rec.dept_name := p_obj_dept.dept_name;
D(v_dept_rec);
END;
PROCEDURE D(p_dept_rec t_dept_rec) IS BEGIN NULL; END;
END;
/
Note that there isn't any 'convenient' way to create one object/record from another, even if they have identical members (such a t_obj_dept and t_dept_rec in the example above). You must copy across the values of all the fields individually.
You say that t_emp_tab is a "PLSQL Table of binary_integer". I'm guessing you mean that it's a PL/SQL Table of some type indexed by binary_integer, as it's far more common to index these tables by binary_integer than it is to store binary_integers in them. For the example above I've assumed that it's a table of t_obj_emps. If it's not, you'll have to map the t_obj_emp object to whatever type of object or record t_emp_tab is a table of.

How to populate an array in an oracle stored procedure?

How to use array( Varray) in store procedure. Actually,i have make a stored procedure from which i retrieve a list of elements.
For example:
create or replace procedure GetTargetFields ( fileformat in varchar2,
filefields out Varray(4) )
IS
BEGIN
SELECT id
INTO filefields
FROM tablename;
END;
use BULK COLLECT INTO:
SQL> CREATE OR REPLACE TYPE vrray_4 AS VARRAY(4) OF VARCHAR2(10);
2 /
Type created
SQL> CREATE OR REPLACE PROCEDURE GetTargetFields(fileformat IN VARCHAR2,
2 filefields OUT vrray_4) IS
3 BEGIN
4 SELECT dummy BULK COLLECT INTO filefields FROM dual;
5 END;
6 /
Procedure created
SQL> DECLARE
2 x vrray_4;
3 BEGIN
4 GetTargetFields(NULL, x);
5 END;
6 /
PL/SQL procedure successfully completed
Also make sure that your query doesn't return more than 4 rows (for a VARRAY(4)) or you will run into ORA-22165
Niraj. You should use the principles Vincent provided, but I suggest you use nested table type instead of varray in case you don't need exactly varray type in your logic. This will save you from ORA-22165 error if the query returns more then 4 rows - nested tabled will be automatically expanded to the size needed. You define nested table type as follows:
declare
type TStrTab is table of varchar2(10);
fStrTab TStrTab := TStrTab();
begin
select ... bulk collect into fStrTab from...
end;
More information about PL/SQL collection types can be found in official Oracle PL-SQL User's Guide and Reference Chapter 5.
Two things:
You need to declare a named type -- you can't use VARRAY directly in a parameter declaration. (Unless this has changed in 11g.)
You need to use BULK COLLECT to use a single query to populate a collection.
Example:
CREATE TYPE fieldlist AS VARRAY(4) OF NUMBER;
CREATE PROCEDURE GetTargetFields( filefields OUT fieldlist )
AS
BEGIN
SELECT id BULK COLLECT INTO filefields FROM tablename;
END;

Resources