Skip to content Skip to sidebar Skip to footer

Call Stored Procedure Containing Collection Of Records Using Callproc In Python

I am having a stored procedure in sql : PROCEDURE sp_add_object_tags( pi_account_id IN  user_files.account_id%TYPE, pi_object_id IN  object_metada

Solution 1:

You may not directly use collections.namedtuple as Oracle object type.

I tried the below on Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 with Cx-Oracle-7.0.0 and Python 3.6.4 and it works for me.

Create TYPE

CREATE OR REPLACE TYPE object_tag_rec AS OBJECT (
     tag_type       VARCHAR2(1),
     tag_category   VARCHAR2(100),
     tag_key        VARCHAR2(250),
     tag_value      VARCHAR2(250),
     created_by     VARCHAR2(50)
);
/

Create collection

CREATE OR REPLACE TYPE object_tag_tbl is TABLE OF object_tag_rec;
/

Create Procedure

CREATE OR REPLACE procedure  sp_add_object_tags(
pi_account_id               IN  INT,
pi_object_id                IN  INT,
pi_all_tags_identified      IN  VARCHAR2,
pi_object_tag_tbl           IN  object_tag_tbl,
po_error_code               OUT NUMBER,
po_error_message            OUT VARCHAR2 )
AS 
BEGIN

   po_error_code    := 0;
   po_error_message := 'NO ERRORS';
END;
/

code

import cx_Oracle
conn = cx_Oracle.connect('user/pwd@//localhost:1521/dbname')
cur = conn.cursor()

recordTypeObj = conn.gettype("HR.OBJECT_TAG_REC") #mind the cases
tableTypeObj  = conn.gettype("HR.OBJECT_TAG_TBL")
params = tableTypeObj.newobject()

rec = recordTypeObj.newobject()
(rec.TAG_TYPE,rec.TAG_CATEGORY,rec.TAG_KEY,rec.TAG_VALUE,rec.CREATED_BY) = ("S","person","person","1","abc")  
#mind the cases for attributes.

po_error_code    = cur.var(cx_Oracle.NUMBER)
po_error_message = cur.var(cx_Oracle.STRING)

params.append(rec)
cur.callproc('dbms_output.enable')
cur.callproc('hr.sp_add_object_tags', [1234, 5678, 'N', params, po_error_code, po_error_message])

print (po_error_code.getvalue(),po_error_message.getvalue())

Execution

$python pass_obj.py
0.0 NO ERRORS

Post a Comment for "Call Stored Procedure Containing Collection Of Records Using Callproc In Python"