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

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.

Related

Create procedure and Insert values in Table

Create a procedure named insert_credit to insert the values in the credit_card table by passing 5 inputs as parameters.
Procedure name: insert_credit
Input parameter : credit_id with data type as number,credit_card_number with data type as varchar,credit_card_expire with data type as varchar,holder_name with data type as varchar and card_type with data type as varchar
Table used: credit_card
I wrote this :
CREATE OR REPLACE PROCEDURE insert_credit(
p_credit_id IN credit_card.credit_id%TYPE,
p_credit_card_number IN credit_card.credit_card_number%TYPE,
p_credit_card_expire IN credit_card.credit_card_expire%TYPE,
p_holder_name IN credit_card.holder_name%TYPE,
p_card_type IN credit_card.card_type%TYPE)
IS
BEGIN
INSERT INTO credit_card ("credit_id", "credit_card_number", "credit_card_expire", "holder_name","card_type")
VALUES (p_credit_id, p_credit_card_number,p_credit_card_expire,
p_holder_name,p_card_type);
COMMIT;
END;
/
On executing I am getting :
" Warning: Procedure created with compilation errors."
CREATE OR REPLACE
PROCEDURE insert_credit(
credit_id IN credit_card.id%TYPE,
credit_card_number IN credit_card.card_number%TYPE,
credit_card_expire IN credit_card.card_expire%TYPE,
holder_name IN credit_card.name%TYPE,
card_type IN credit_card.cc_type%TYPE) AS
BEGIN
INSERT INTO credit_card(id,card_number,card_expire,name,cc_type)
VALUES(credit_id,credit_card_number,credit_card_expire,holder_name,card_type);
END insert_credit;
/
if you want to debug your procedure you can run it like a anonymous block and all declare all input parameter.
/*CREATE OR REPLACE PROCEDURE insert_credit(
p_credit_id IN credit_card.credit_id%TYPE,
p_credit_card_number IN credit_card.credit_card_number%TYPE,
p_credit_card_expire IN credit_card.credit_card_expire%TYPE,
p_holder_name IN credit_card.holder_name%TYPE,
p_card_type IN credit_card.card_type%TYPE)
IS */
declare
p_credit_id credit_card.credit_id%TYPE := somevalue
...
..
...
P_card_type
BEGIN
INSERT INTO credit_card ("credit_id", "credit_card_number", "credit_card_expire", "holder_name","card_type")
VALUES (p_credit_id, p_credit_card_number,p_credit_card_expire,
p_holder_name,p_card_type);
COMMIT;
END;
Now you will be able to get the line where you are getting error. After the block run without any error you can remove the declaration part and uncomment the commented code.
CREATE OR REPLACE PROCEDURE insert_credit(
credit_id NUMBER,
credit_card_number VARCHAR,
credit_card_expire VARCHAR,
holder_name VARCHAR,
card_type VARCHAR)
AS
/*Declaration block*/
BEGIN
INSERT INTO credit_card(id,card_number,card_expire,name,cc_type)
VALUES(credit_id,credit_card_number,credit_card_expire,holder_name,card_type);
END insert_credit;
/

create a collection with 2 columns and insert all the rows at once PL/SQL

I have reference data which I want to use in a PL/SQL package.
This is a collection with two columns:
type table_info IS RECORD (
table_name VARCHAR2(50),
join_column VARCHAR2(50)
);
type config_tables_type is table of table_info; -- list of the config tables
I would like to add several rows at once to this collection, I tried this:
config_tables config_tables_type := config_tables_type (table_info('Commands','object_id'),
table_info('Contact_notificationcommands','command_object_id'),
table_info('Contactgroup_members','contact_object_id'),
table_info('Contactgroups','contact_object_id'),
table_info('Contactnotificationmethods','command_object_id'),
table_info('customvariables','object_id'),
table_info('Host_contactgroups','host_id'),
table_info('Host_contacts','host_id'),
table_info('Hostescalation_contactgroups','contactgroup_object_id'),
table_info('Hostescalation_contacts','contact_object_id'),
table_info('Host_parenthosts','parent_host_object_id'),
table_info('Hostdependencies','host_object_id'),
table_info('Hostdependencies','dependent_host_object_id'),
table_info('Hostescalations','host_object_id'),
table_info('Hostgroup_members','host_object_id'),
table_info('Hostgroups','hostgroup_object_id'),
table_info('Hosts','host_object_id'),
table_info('Service_contactgroups','contactgroup_object_id'),
table_info('Service_contacts','contact_object_id'),
table_info('Servicedependencies','service_object_id'),
table_info('Serviceescalation_contactgroups','contactgroup_object_id'),
table_info('Serviceescalation_contacts','contact_object_id'),
table_info('Serviceescalations','service_object_id'),
table_info('Servicegroup_members','service_object_id'),
table_info('Servicegroups','servicegroup_object_id'),
table_info('Services','service_object_id'),
table_info('Timeperiods','timeperiod_object_id')
);
But I have the following compilation error:
PLS-00222: no function with name 'TABLE_INFO' exists in this scope.
This type is declared in the package description and the initialisation of the config_tables collection is done in the package body.
Thanks
The below "hack" should do the trick!
declare
type table_info IS RECORD (
table_name VARCHAR2(50),
join_column VARCHAR2(50)
);
type config_tables_type is table of table_info;
config_tables config_tables_type;
function table_info_constructor(table_name VARCHAR2, join_column VARCHAR2) return table_info
is
t_i table_info;
begin
t_i.table_name := table_name;
t_i.join_column := join_column;
return(t_i);
end;
begin
config_tables := config_tables_type(table_info_constructor('Commands','object_id'),
table_info_constructor('Contact_notificationcommands','command_object_id'),
table_info_constructor('Contactgroup_members','contact_object_id'),
table_info_constructor('Contactgroups','contact_object_id'),
table_info_constructor('Contactnotificationmethods','command_object_id'),
table_info_constructor('customvariables','object_id'),
table_info_constructor('Host_contactgroups','host_id'),
table_info_constructor('Host_contacts','host_id'),
table_info_constructor('Hostescalation_contactgroups','contactgroup_object_id'),
table_info_constructor('Hostescalation_contacts','contact_object_id'),
table_info_constructor('Host_parenthosts','parent_host_object_id'),
table_info_constructor('Hostdependencies','host_object_id'),
table_info_constructor('Hostdependencies','dependent_host_object_id'),
table_info_constructor('Hostescalations','host_object_id'),
table_info_constructor('Hostgroup_members','host_object_id'),
table_info_constructor('Hostgroups','hostgroup_object_id'),
table_info_constructor('Hosts','host_object_id'),
table_info_constructor('Service_contactgroups','contactgroup_object_id'),
table_info_constructor('Service_contacts','contact_object_id'),
table_info_constructor('Servicedependencies','service_object_id'),
table_info_constructor('Serviceescalation_contactgroups','contactgroup_object_id'),
table_info_constructor('Serviceescalation_contacts','contact_object_id'),
table_info_constructor('Serviceescalations','service_object_id'),
table_info_constructor('Servicegroup_members','service_object_id'),
table_info_constructor('Servicegroups','servicegroup_object_id'),
table_info_constructor('Services','service_object_id'),
table_info_constructor('Timeperiods','timeperiod_object_id')
);
end;
Let me know how it works out for you.
For any further clarifications don't hesitate to ask me.
Ted.
There is an another alternative where you can basically create a schema level object i.e OBJECT type and TABLE Type and then call it in the plsql block as shown below. Hope this helps too.
--Create object type
CREATE OR REPLACE TYPE table_info
IS
OBJECT
(
table_name VARCHAR2(50),
join_column VARCHAR2(50))
/
--Create table type on Object type
CREATE OR REPLACE TYPE config_tables_type
IS
TABLE OF table_info
/
--PLSQL block of code
DECLARE
config_tables config_tables_type;
BEGIN
config_tables config_tables_type := config_tables_type (table_info('Commands','object_id'),
table_info('Contact_notificationcommands','command_object_id'),
table_info('Contactgroup_members','contact_object_id'),
table_info('Contactgroups','contact_object_id'),
table_info('Contactnotificationmethods','command_object_id'),
table_info('customvariables','object_id'),
table_info('Host_contactgroups','host_id'),
table_info('Host_contacts','host_id'),
table_info('Hostescalation_contactgroups','contactgroup_object_id'),
table_info('Hostescalation_contacts','contact_object_id'),
table_info('Host_parenthosts','parent_host_object_id'),
table_info('Hostdependencies','host_object_id'),
table_info('Hostdependencies','dependent_host_object_id'),
table_info('Hostescalations','host_object_id'),
table_info('Hostgroup_members','host_object_id'),
table_info('Hostgroups','hostgroup_object_id'),
table_info('Hosts','host_object_id'),
table_info('Service_contactgroups','contactgroup_object_id'),
table_info('Service_contacts','contact_object_id'),
table_info('Servicedependencies','service_object_id'),
table_info('Serviceescalation_contactgroups','contactgroup_object_id'),
table_info('Serviceescalation_contacts','contact_object_id'),
table_info('Serviceescalations','service_object_id'),
table_info('Servicegroup_members','service_object_id'),
table_info('Servicegroups','servicegroup_object_id'),
table_info('Services','service_object_id'),
table_info('Timeperiods','timeperiod_object_id')
);
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.

Oracle PL/SQL Developer: Return %RowType from Package Procedure

i'm kind of new to Oracle Pl\SQL. I was just trying to create a simple Package with a procedure that returns a set of object id's; the code is as follows:
--Package Spec
CREATE OR REPLACE PACKAGE TEST IS
--GET OBJECT ID'S FROM CONTROL TABLE
PROCEDURE get_object_id_control(p_obj_id OUT abc_table%ROWTYPE);
END;
--Package Body
PROCEDURE get_object_id_control(p_obj_id OUT abc_table%ROWTYPE) AS
BEGIN
SELECT object_id
INTO p_obj_id
FROM abc_table
WHERE fec_proc IS NULL;
END;
I get Error: PL/SQL: ORA-00913: too many values. Is this the correct way for returning multiple values of same data type, or is there a better approach. Thanks in advance.
You can create a custom table type and set the out parameter of the procedure to that type.
CREATE TABLE ABC_TABLE(ID varchar2(100));
create or replace type abc_tab is table of varchar2(100);
/
CREATE OR REPLACE PACKAGE TEST IS
PROCEDURE get_object_id_control(p_obj_id OUT abc_tab);
END;
/
CREATE OR REPLACE PACKAGE BODY TEST IS
PROCEDURE get_object_id_control(p_obj_id OUT abc_tab) AS
BEGIN
SELECT id
bulk collect INTO p_obj_id
FROM abc_table;
END;
END;
/
Then you can call it like so:
declare
v abc_tab;
begin
TEST.get_object_id_control(p_obj_id => v);
for i in v.first..v.last loop
dbms_output.put_line(v(i));
end loop;
end;
/
Similar to GurV's answer (since he beat me by like 30 seconds...), you can use a PL/SQL object type as well. You do not need the CREATE TYPE statement if you don't need to reference the type in SQL.
--Package Spec
CREATE OR REPLACE PACKAGE TEST AS
TYPE id_table_type IS TABLE OF NUMBER;
--GET OBJECT ID'S FROM CONTROL TABLE
PROCEDURE get_object_id_control(p_obj_id_list OUT id_table_type);
END;
--Package Body
CREATE OR REPLACE PACKAGE BODY TEST AS
PROCEDURE get_object_id_control(p_obj_id_list OUT id_table_type) AS
BEGIN
SELECT object_id
BULK COLLECT INTO p_obj_id_list
FROM abc_table
WHERE fec_proc IS NULL;
END;
END;
To use it:
DECLARE
l_id_list test.id_table_type;
BEGIN
test.get_object_id_control (p_obj_id_list => l_id_list);
FOR i IN l_id_list.FIRST .. l_id_list.LAST LOOP
DBMS_OUTPUT.put_line (l_id_list (i));
END LOOP;
END;

Oracle PL/SQL: How to print a table type

I am trying to print an table type for debugging purposes, but don't know how. I tried the following two methods, neither of which work:
dbms_output.put_line (V_TEMP_TABTYPE(1));
dbms_output.put_line (V_TEMP_TABTYPE);
The error generated is: PLS-00306: wrong number or types of arguments in call to.
So, how can I print the contents of a table type? Or is there a different way to display the contents?
The table_type and the type it references are::
create or replace TYPE MY_TYPE IS OBJECT( MyString Varchar(20)
, counter Number(9) );
create or replace TYPE MY_TABTYPE AS TABLE OF MY_TYPE;
Oracle has objects but it's ... different. Not exactly sure with your question if you want to see the values of the properties or if you want to actually see the type.
CREATE OR REPLACE TYPE MY_TYPE IS OBJECT (
MyString Varchar(20)
, counter Number(9)
);
Now run some code for it.
DECLARE
myType MY_TYPE;
BEGIN
myType := MY_TYPE('ABC123',0);
-- To see the values reference the properties
DBMS_OUTPUT.PUT_LINE(myType.mystring);
-- To see they TYPE of the OBJECT
DBMS_OUTPUT.PUT_LINE(SYS.ANYDATA.CONVERTOBJECT(myType).getTypeName());
END;
Of course you can create methods on the object to return information for you a bit easier.
CREATE OR REPLACE TYPE MY_TYPE IS OBJECT (
MyString Varchar(20)
, counter Number(9)
, MEMBER FUNCTION getType RETURN VARCHAR2
, MEMBER FUNCTION toString RETURN VARCHAR2
)
/
CREATE OR REPLACE TYPE BODY MY_TYPE
AS
MEMBER FUNCTION getTYPE RETURN VARCHAR2 IS
BEGIN
RETURN SYS.ANYDATA.CONVERTOBJECT(SELF).getTypeName();
END;
MEMBER FUNCTION toString RETURN VARCHAR2 IS
BEGIN
RETURN 'MY_TYPE('||self.mystring||','||self.counter||')';
END;
END;
/
You can call the functions on the object now makes it easier to read imo.
DECLARE
mytype MY_TYPE;
BEGIN
mytype := MY_TYPE('AGAIN','0');
DBMS_OUTPUT.PUT_LINE(mytype.toString);
DBMS_OUTPUT.PUT_LINE(mytype.getType);
END;
dbms_output.put_line(v_temp_tabtype(i).myString);

Resources