How to pass long json with more than 60000 characters - oracle

I have created a stored procedure which does insertion into a table. How can I insert json data which can be up to 64KB? The column request_params is of type blob.
create or replace PROCEDURE INSERT_USER
(in_user_id IN VARCHAR2,
in_user_name IN VARCHAR2,
in_request_params IN VARCHAR2)
AS
BEGIN
insert into user_table(user_id,user_name,request_params)
values(in_user_id,in_user_name,in_request_params);
END INSERT_USER;

The column request_params is of type blob.
Then use a BLOB:
create or replace PROCEDURE INSERT_USER(
in_user_id IN VARCHAR2,
in_user_name IN VARCHAR2,
in_request_params IN BLOB
)
AS
BEGIN
insert into user_table(user_id,user_name,request_params)
values(in_user_id,in_user_name,in_request_params);
END INSERT_USER;
/
or the same types as the table's columns (which would also be a BLOB):
create or replace PROCEDURE INSERT_USER(
in_user_id IN user_table.user_id%TYPE,
in_user_name IN user_table.user_name%TYPE,
in_request_params IN user_table.request_params%TYPE
)
AS
BEGIN
insert into user_table(user_id,user_name,request_params)
values(in_user_id,in_user_name,in_request_params);
END INSERT_USER;
/
how to pass blob from JAVA applictain ? I mean can I covert JSON string to blob in JAVA
Use the PreparedStatement.setBinaryStream or PreparedStatement.setBlob functions. An example is in this answer (and if you have a JSON string then just get the bytes from the string and stream that to the blob).

Related

I am getting comma seperated IDs and I have to pass that in where clause inside Oracle stored procedure

I am passing comma separated string in proc execution
create replace proc(trasactionID in varchar2,
user_data out refcursor,
succdata out varchar2,
errodata out varchar2)
begin
open user_data for
select name,id from table a where id in(trasactionID);
end;
/

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;
/

How to convert XMLTYPE in VARCHAR in ORACLE?

I have two columns in my table(TRANSACTION) in ORACLE which are XMLTYPE(XML_IN and XML_OUT). My procedure is not working because I don't know how to convert them to VARCHAR or something(I just think that this is the error). My procedure is:
PROCEDURE SEARCH_XML
(
P_ID_TRANSACTION IN TRANSACTION.ID_TRANSACTION%TYPE,
P_CURSOR OUT T_CURSOR
)
IS
BEGIN
OPEN P_CURSOR FOR
SELECT T.XML_IN, T.XML_OUT
FROM TRANSACTION T
WHERE T.ID_TRANSACTION = P_ID_TRANSACTION;
END SEARCH_XML;
When I call this procedure error message in VisualStudio2008 is: "Unsupported oracle data type USERDEFINED encountered." Any idea how is this working?
XMLType has two methods: getStringVal() and getClobVal() which will convert the XML structure to their string representations (as a VARCHAR2 and CLOB respectively). Unless you know that your XML output is going to always be less than 4000 characters (bytes) then you will probably want to use getClobVal() like this:
PROCEDURE SEARCH_XML
(
P_ID_TRANSACTION IN TRANSACTION.ID_TRANSACTION%TYPE,
P_CURSOR OUT T_CURSOR
)
IS
BEGIN
OPEN P_CURSOR FOR
SELECT T.XML_IN.getClobVal() AS XML_IN,
T.XML_OUT.getClobVal() AS XML_OUT
FROM TRANSACTION T
WHERE T.ID_TRANSACTION = P_ID_TRANSACTION;
END SEARCH_XML;

Oracle cast PL/SQL table to SYS_REFCURSOR

I am trying to access one my Stored Procedure from java code, where the procedure is returing a PL/SQL table(PACKAGE TABLE) type, as it is easy to handle SYS_REFCURSOR in my java code, I am trying to convert the PL/SQL table to SYS_REFCURSOR in my Stored Procedure. After googling I didn't got any appropriate answer for this conversion. Can someone help me out for this conversion logic?
create or replace PROCEDURE TESTPROC(
INPUT1 IN VARCHAR2,
INPUT2 IN VARCHAR2,
P_PRC OUT SYS_REFCURSOR) AS
PACKAGE_TABLE PACKAGE.TESTTABLE;
BEGIN
PACKAGE_TABLE := FUNCTION_RETURN_PACKAGE_TABLE(INPUT1, INPUT2);
-- **LOGIC TO CONVERT PACAKGE_TABLE TO SYS_REFCURSOR GOES HERE**
END TESTPROC;
You can use TABLE operator for this
create or replace PROCEDURE TESTPROC(
INPUT1 IN VARCHAR2,
INPUT2 IN VARCHAR2,
P_PRC OUT SYS_REFCURSOR) AS
BEGIN
OPEN P_PRC FOR
SELECT * FROM TABLE(FUNCTION_RETURN_PACKAGE_TABLE(INPUT1, INPUT2));
END TESTPROC;
But you should keep in mind that you have to have schema level pl\sql table type (for oracle <12c). Also notice that SELECT * FROM brings you one-feild rows with your-plsql-table-row-type value.

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.

Resources