Groovy Oracle XMLTYPE - oracle

I have a procedure in Oracle that looks like this.
PROCEDURE p_main_xml (p_term IN VARCHAR2
, p_crn IN VARCHAR2 DEFAULT NULL
, p_pin IN VARCHAR2 DEFAULT NULL
, p_hash IN VARCHAR2 DEFAULT NULL
, p_updated IN VARCHAR2 DEFAULT NULL
, p_xml_clob OUT xmltype)
I am trying to call this from a grails app and get the XML. The result it 8meg or so.
My call looks like this.
sql.call '{call cl_sectiondownload_pk.p_main_xml(?,?,?,?,?,?)}', [termCode, crn, pin, hash, updated, java.sql.Types.SQLXML], { p_xml_clob ->
//println "this is my sql: " + p_json
xmlOut = p_xml_clob
}
I am getting this error from Oracle.
wrong number or types of arguments in call to 'P_MAIN_XML'
Details:
Grails Version: 4.0.10
JVM Version: 11.0.2
Oracle 12.1.0.2.0

XML in oracle is quite specific object
unfortunately don't have oracle right now but something like this should work
sql.call('''
DECLARE
x XMLTYPE;
BEGIN
cl_sectiondownload_pk.p_main_xml(?,?,?,?,?,x);
?:=x.getCLOBVal();
END;
''', [.., sql.VARCHAR]){x-> ... }
maybe instead of sql.VARCHAR use sql.CLOB
assume that sql is an instance of groovy.sql.Sql

Related

Convert integer to hours and minutes - getting PL/SQL numeric or value error

I am getting
ORA-06502 PL/SQL numeric or value error
when I am trying to execute this statement from Report Builder 6i and Oracle database 11g.
select to_char(to_date(lateby, 'sssss'), 'mi:ss')
from attendancelogs
where emp_no = :emp_no
and attendancedate = :dat;
late number(15);
The same statement when run under SQL*Plus is running fine without any error and fetching values the way I want.
Any help will be much appreciated.
Regards,
Mac
It might be a Reports bug, I don't know, but as a workaround you could try wrapping this into a stored procedure in the database as follows:
CREATE OR REPLACE FUNCTION sssss_to_miss ( p1 in varchar2 ) RETURN VARCHAR2 IS
BEGIN
RETURN ( to_char(to_date(p1, 'sssss'), 'mi:ss') );
END;
/
Then in the Reports Code:
select sssss_to_miss (lateby)
from attendancelogs
where emp_no = :emp_no
and attendancedate = :dat;
late number(15);
Alternatively, something like (not tested):
select to_char(to_number(lateby)/60,'00') || ':' || to_char(mod(to_number(lateby),60),'00')
from attendancelogs
where emp_no = :emp_no
and attendancedate = :dat;

PL/SQL array into refcursor to return

I'm new to PL/SQL. I need to convert an array into a refcursor to return the record set. this is how SP looks like:
PROCEDURE EXAMPLE(p_recordset OUT SYS_REFCURSOR) AS
TYPE COUNTRY_ARR IS TABLE OF VARCHAR2(60) INDEX BY BINARY_INTEGER;
V_COUNTRY_ARR COUNTRY_ARR;
BEGIN
V_COUNTRY_ARR(1) := 'US';
V_COUNTRY_ARR(2) := 'AUS';
V_COUNTRY_ARR(3) := 'NA';
OPEN p_recordset FOR SELECT * FROM TABLE(CAST(V_COUNTRY_ARR AS COUNTRY_ARR));
END EXAMPLE;
Gives me an error saying 'Error(86,68): PL/SQL: ORA-00902: invalid
datatype'
Thank you in advance!
You are trying to cast to a PL/SQL type. That cannot be done until Oracle 12c (and, even then, the type would need to be defined in a package specification, I believe).
In Oracle 11g, you need to create COUNTRY_ARR as a SQL type with the CREATE TYPE command. Then your cast will work.
I.e., (outside of your PL/SQL package)
CREATE TYPE country_arr AS TABLE OF VARCHAR2(60);
Sorry I don't have an Oracle database handy to give more complete/exact syntax.

Toad Error when trying to debug a package with XMLTYPE as a parameter

When I try to debug a package, which has XMLTYPE as a input data i get a access violation error.
Access violation at address 00409664 in module 'Toad.exe'. Read of address 0000901B
The versions are listed below:
Oracle 11g Enterprise Edition Release 11.2.0.4.0
Oracle Client : 11.2.0.1.0
Toad : 11.6.1.6
The package I created is given below:
/**************************************************/
CREATE OR REPLACE PACKAGE SA.Cust_Orders AS
PROCEDURE save_order
(p_data IN XMLTYPE
, p_return_code OUT NUMBER
, p_status_msg OUT VARCHAR2);
END Cust_Orders;
CREATE OR REPLACE PACKAGE BODY SA.Cust_Orders AS
PROCEDURE save_order
(p_data IN XMLTYPE
, p_return_code OUT NUMBER
, p_status_msg OUT VARCHAR2) IS
BEGIN
p_return_code := 0;
p_status_msg := 'Data has been successfully saved.';
END save_order;
END Cust_Orders;
/**************************************************/
If I remove the "p_data" from the parameters I am able to debug.

Oracle Stored Procedure not Returning Results

My Oracle Stored Procedure does not return any results. I have tested the query outside the SPROC and it seems to return values. I have a feeling it has something to do with how I'm passing the sample and test parameters - because if I hardcode the sampleNumber and testNumber parameters in the Stored Proc, the results are returned correctly. I have pasted the Procedure and the PLSQL Declaration. I am very new to writing Oracle Procedures and any help will be greatly appreciated. Thank you.
CREATE OR REPLACE PROCEDURE TESTANALYSIS
(vOrderNumber IN VARCHAR2
, vTestNumber IN NUMBER
, vSampleNumber IN NUMBER
)
AS
anVar VARCHAR2(50) := NULL;
BEGIN
SELECT T.ANALYSIS INTO anVar FROM TEST T JOIN SAMPLE S
ON T.SAMPLE_NUMBER = S.SAMPLE_NUMBER
WHERE T.TEST_NUMBER = vTestNumber
AND S.SAMPLE_NUMBER = vSampleNumber
AND S.ORDER_NUM = vOrderNumber;
dbms_output.put_line(anVar);
END TESTANALYSIS;
--PL SQL Call
SET SERVEROUTPUT ON
EXEC TESTANALYSIS('4200010061', 720000050516, 14789)
/

How to execute an Oracle function that returns a sys_refcursor using NHibernate?

This is the function:
FUNCTION GET_ALL(P_USER_ID IN VARCHAR2) RETURN SYS_REFCURSOR IS
C SYS_REFCURSOR;
BEGIN
OPEN C
FOR 'SELECT * FROM XYZ WHERE USER_ID = :P_USER_ID'
USING P_USER_ID;
RETURN C;
END;
I'm trying to call this function using NHibernate, like this:
Session
.CreateSQLQuery("BEGIN ? = PKG.GET_ALL(:P_USER_ID); END;")
.SetString("P_USER_ID", "SOMEONE")
.List<XYZ>();
Any code, tips or smoke signs are welcome.
PS: I'm using NHibernate 3.3.0.GA
From the official docs:
For Oracle the following rules apply:
A function must return a result set. The first parameter of a
procedure must be an OUT that returns a result set. This is done by
using a SYS_REFCURSOR type in Oracle 9 or 10. In Oracle you need to
define a REF CURSOR type, see Oracle literature.
There are working tests with full mapping and stored procedure code at https://github.com/nhibernate/nhibernate-core/tree/master/src/NHibernate.Test/SqlTest/Custom/Oracle

Resources