Portable way to call Stored Procs. which return a ResultSet (MySQL/Oracle)?
Hello!
I have developed a Java application which calls stored procedures, some of which return a ResultSet (always one).
I need to support more DBMS vendors, so I used the standard JDBC syntax to call the stored procedures ({call storedProcedure(?,?)}).
I developed the code using MySQL, but succeded to call SQL Server 2000 stored procedures changing only the driver (that's the purpose of JDBC, right?).
Now I need to support the Oracle DBMS, but I can't find a way to create a stored procedure which returns a result set and allows me not to change the already stable Java code.
How could I achieve this result? Should I use some other Oracle object, say a function?
By the way, I searched the Internet, but only found solutions which would've made me change code. This is definitely not portable
Thank you very much for your help!
PS: Some of the stored procedures return both a ResultSet AND OUT parameters.
Hello and thanks for your reply, jwenting.
However, keeping useless "philosophy" out of this thread, please just let me clarify that I obviously didn't want portability of stored procedures, I just wanted a standard JDBC to interface with them. I haven't had problems in the past with stored procedures and other DBMSs, I'm having them just now with the Oracle JDBC driver. Since JDBC is meant to be portable, I hoped there was a portable way of calling Oracle stored procedures (or whatever else in Oracle takes inputs and returns output and a ResultSet...). Unfortunately, there doesn't seem to be any, unless I use a commercial driver from DataDirect. I find this a heavy limitation of the Oracle JDBC driver, but, alas, here it is... I'll have to refactor my stable code...
Thanks again...
Similar Messages
-
Calling stored procedure and returning multiple resultsets
Hello,
Is it possible to create a procedure that return multiple result sets?
e.g.
procedure GetDataFromTables() is
begin
select * from table_one;
select * from table_two;
end GetDataFromTables;
And I want to call this procedure that returns multiple resultsets (one for table_one and another for table_two)
I have referred to the OCCI sample occiproc.cpp, but I am not sure how to handle multiple resultsets.
Your help is highly appreciated.Thank You,
Got the REF cursor in storedproc.cpp.
But as it is documented, getCursor() gets the REF CURSOR value of an OUT parameter as a ResultSet.
So, is it true that if I have to write a procedure that selects * from 50 tables (50 SQL statements) , I have to set 50 OUT parameters to get the result sets.?
Is there any better way of doing this?
e.g. in DB2, we can open multiple cursors in the procedure body and then get the result set one after another.
CREATE PROCEDURE get_staging_data ()
RESULT SETS 1 <no. of result sets>
LANGUAGE SQL
BEGIN ATOMIC
DECLARE L_SQL varchar(5000);
DECLARE c CURSOR WITH RETURN TO CLIENT FOR L_STMT;
SET L_SQL = '';
SET L_SQL = 'SELECT * FROM ';
SET L_SQL = L_SQL || IN_Tab_Name ;
SET L_SQL = L_SQL || ' WHERE VERIFY_FLAG=''S'' FOR READ ONLY OPTIMIZE FOR 2000 ROWS' ;
PREPARE L_STMT FROM L_SQL;
OPEN c; < can open multiple cursors in the prodedure body>
END -
Calling Stored Procedure which returns single or multiple value
Hi All Experts,
I have written one stored procedure which is returning different
integer values according to condition.I am calling this
stored procedure through prepareCall() method then executeUpdate() method ,but it is always returning 1,not my desired value.
Pl also tell me,if my stored procedure returns more than one value
then how will i get those values in my java program?
Pl help by writing some sample code.
Thanx in Advance.
Pradipto1) Create CallableStatement
2) For each of returning value you have to call RegisterOutParameter()
3) For others user setXXX() method depending on type of parameter
4) call
5) use vars registered through RegisterOutParameter() - all your values should be there
Pavel -
Entity framework-calling stored Proc to return one param returns ORA-6550
Hi There,
I am new to the VS2010/MVC3 game with ODP.net, so no abuse please .......
I have successfully inserted a record into a table via the controller/entity framework, but only by currently hard coding the id. The next logical step is to attempt to obtain the id from the Oracle DB via a stored procedure. Here is the body of the PL/SQL procedure on the Oracle side:-
create or replace package body dotnetcommon as
PROCEDURE getnextpoid ( p_to_id OUT NUMBER ) IS
BEGIN
SELECT pord_seq.nextval INTO p_to_id FROM dual;
END getnextpoid;
end dotnetcommon;
show error
I have then added this procedure to my Entity Framework Model within VS2010 , and from within the Model Browser, I now have a stored procedure of "DOTNETCOMMON_GETNEXTPOID" under my Model.Store. This has an out type parameter named p_to_id, which is a type of number.
I then added a function import, so I have a function by the name of "GETNEXTPOID" under the Function Imports folder, with a collection property of 'None'.
This function has a parameter P_TO_ID with a type of "Decimal".
From within my controller, I have the following code:-
[HttpPost]
public string Create(PURCHASEORDER po)
TasEntities tmprep = new TasEntities();
ObjectParameter poid = new ObjectParameter("poId", typeof(Int16));
var result = tmprep.GETNEXTPOID(poid);
return "Next PO Id is " + result;
Thus, on pressing the Save from my view, i am hoping to return the next logical sequence number into the poid objectparameter. However, I am presented with the following error:-
Inner Exception of:-
{"ORA-06550: line 1, column 8:\nPLS-00306: wrong number or types of arguments in call to 'GETNEXTPOID'\nORA-06550: line 1, column 8:\nPL/SQL: Statement ignored"}
It falls over , taking me to the Model.Designer.cs at :-
public int GETNEXTPOID(ObjectParameter p_TO_ID)
return base.ExecuteFunction("GETNEXTPOID", p_TO_ID);
I am assuming it is to do with the decimal/int/number conversion, but I do not have the nouse to fix it ? Does anybody know what is wrong with what I have attempted to do above ?
Please please get back to me if you need any more information, I am wanting to move on but cannot.
I have seen posts regarding setting the sequence in a before insert trigger on the table, but this is not ideal. Can the above not work instead ?
Many thanks in advance for any help you can provide.
Graeme
Edited by: user4487499 on 05-Apr-2011 08:18I have now raised this in the odp.net forum, as on looking at other posts it seems to be the best place for it.
Cheers,
Graeme
Edited by: user4487499 on 05-Apr-2011 08:18
Edited by: user4487499 on 05-Apr-2011 13:00 -
Calling a stored procedure which returns a value
Hi Friends,
I want to call a stored procedure which returns a value.
Eg
create or replace procedure xyz(a1 in varchar2, a2 in varchar2, z1 out number)
thanksHi,
use this.
declare
retval number;
begin
abc('aaa','bbb',retval);
dbms_output.put_line('retval is ' ||retval);
end;
--Basava.S -
Writing a Stored Proc which will execute the Queries passed to it as args
Hi,
I need to write a Stored Proc which gets the Query to be executed as an Argument and return the results in an OUT Variable.
The Stored Proc i wrote is
CREATE OR REPLACE PROCEDURE SP_FETCH_RESULTS(l_query in varchar2,
l_returnValue out clob )
is
l_output utl_file.file_type;
l_theCursor integer ;
l_columnValue clob;
l_status integer;
l_results clob;
begin
l_theCursor := dbms_sql.open_cursor;
-- Parse the Query
dbms_sql.parse( l_theCursor, ':x', dbms_sql.native );
DBMS_SQL.BIND_VARIABLE(l_theCursor, ':x', l_query);
-- Define the coulmnValue Variables
dbms_sql.define_column( l_theCursor, 1, l_columnValue);
-- Execute the Query
l_status := dbms_sql.execute(l_theCursor);
-- Get the Results and prepare the return Value.
loop
exit when ( dbms_sql.fetch_rows(l_theCursor) <= 0 );
dbms_sql.column_value( l_theCursor, 1, l_columnValue );
DBMS_LOB.APPEND(l_returnValue, l_columnValue);
end loop;
-- Close the Cursor
dbms_sql.close_cursor(l_theCursor);
end SP_FETCH_RESULTS;
As the Queries paased to it always return CLOB Types i declared the OUT Variable as CLOB
And when i try to execute is using the below given PL/SQL Block
DECLARE
l_Query CLOB ;
l_retVal CLOB;
l_results CLOB;
BEGIN
l_Query:='SELECT extract(object_value,''/AC'').getStringVal() into l_results from CommonAssetCatalog';
SP_FETCH_RESULTS(l_Query,l_retVal);
dbms_output.put_line ('Results =');
END;
I am getting the Error.
ERROR at line 1:
ORA-00900: invalid SQL statement
ORA-06512: at "SYS.DBMS_SYS_SQL", line 906
ORA-06512: at "SYS.DBMS_SQL", line 39
ORA-06512: at "WEBLOGIC.SP_FETCH_RESULTS", line 13
ORA-06512: at line 7
Could you guys pls give me some inputs on how to resolve this..
Thanks a lot
SateeshHi James,
I tried with out the Bind Variable and got the Same Error..
The Code which i tried earlier is
CREATE OR REPLACE PROCEDURE SP_FETCH_RESULTS(l_query in varchar2,
l_returnValue out clob )
is
l_output utl_file.file_type;
l_theCursor integer ;
l_columnValue clob;
l_status integer;
l_results clob;
begin
l_theCursor := dbms_sql.open_cursor;
-- Parse the Query
dbms_sql.parse( l_theCursor, l_query, dbms_sql.native );
--DBMS_SQL.BIND_VARIABLE(l_theCursor, ':x', l_query);
-- Define the coulmnValue Variables
dbms_sql.define_column( l_theCursor, 1, l_columnValue);
-- Execute the Query
l_status := dbms_sql.execute(l_theCursor);
-- Get the Results and prepare the return Value.
loop
exit when ( dbms_sql.fetch_rows(l_theCursor) <= 0 );
dbms_sql.column_value( l_theCursor, 1, l_columnValue );
DBMS_LOB.APPEND(l_returnValue, l_columnValue);
end loop;
-- Close the Cursor
dbms_sql.close_cursor(l_theCursor);
end SP_FETCH_RESULTS;
and the Procedure got Compiled Successfully and when i ran the PL/SQL Block which calls the Above Stored Proc i got the Error
DECLARE
ERROR at line 1:
ORA-00905: missing keyword
ORA-06512: at "SYS.DBMS_SYS_SQL", line 906
ORA-06512: at "SYS.DBMS_SQL", line 39
ORA-06512: at "WEBLOGIC.SP_FETCH_RESULTS", line 13
ORA-06512: at line 7
Pls let me know how can i resolve this..
Thanks
Sateesh -
Calling Stored procedure which uses Bulk Collect
Hi All, I have Oracle stored procedure which uses Bulk Collect and returns table type parameter as output. Can anyone please help me how Can I call this kind of stored procedures which returns table type output using VB and Oracle's Driver. (I am successfully able to call using MS ODBC driver, but I want to use OraOLEDB driver.)
861412 wrote:
how Can I call this kind of stored procedures which returns table type output using VB and Oracle's Driver. This forum deals with the server-side languages SQL and PL/SQL.
Your question deals with the client side and Visual Basic language. -
SQL Exception: Invalid column index while calling stored proc from CO.java
Hello all,
I am getting a "SQL Exception: Invalid column index" error while calling stored proc from CO.java
# I am trying to call this proc from controller instead of AM
# PL/SQL Proc has 4 IN params and 1 Out param.
Code I am using is pasted below
==============================================
OAApplicationModule am = (OAApplicationModule)oapagecontext.getApplicationModule(oawebbean);
OADBTransaction txn = (OADBTransaction)am.getOADBTransaction();
OracleCallableStatement cs = null;
cs = (OracleCallableStatement)txn.createCallableStatement("begin MY_PACKAGE.SEND_EMAIL_ON_PASSWORD_CHANGE(:1, :2, :3, :4, :5); end;", 1);
try
cs.registerOutParameter(5, Types.VARCHAR, 0, 2000);
cs.setString(1, "[email protected]");
cs.setString(2, s10);
//Debug
System.out.println(s10);
cs.setString (3, "p_subject " );
cs.setString (4, "clob_html_message - WPTEST" );
outParamValue = cs.getString(1);
cs.executeQuery();
txn.commit();
catch(SQLException ex)
throw new OAException("SQL Exception: "+ex.getMessage());
=========================================
Can you help please.
Thanks,
VinodYou may refer below URL
http://oracleanil.blogspot.com/2009/04/itemqueryvoxml.html
Thanks
AJ -
XI calling an Oracle Stored Procedure which returns an Object to XI
I am currently trying to call an Oracle Packaged/Procedure from XI which accepts a couple of parameters as I/O and returns an Object as one of the parameters.
I have created the Object within the Oracle Database CREATE OR REPLACE TYPE xy_jdbc AS OBJECT
column_name type ...etc
CREATE OR REPLACE TYPE xy_tab_jdbc AS TABLE OF xy_jdbc;
One of the parameters for the stored procedure is set up as this type xy_tab_jdbc this will be populated based upon one of the parameters passed into the Package/Procedure.
Is this possible? If it is, how do I map the returned object within XI?Dear Hilary,
the JDBC adapter does not support vendor-specific or non-scalar data types.
Workaround: Change the stored proc's signature not to use an object, but the object's fields instead.
Regards,
Thilo -
Calling stored proc from java to return ref cursor
Hi All,
We have a requirement to display all the records from a table on a JAVA screen. Due to some constraints, we have to write the query in the stored proc. We are trying to return a result set from the stored proc to the java code by using a ref cursor. Please refer to the code below.
Following is the PL/SQL proc ?
procedure sp_user_course2(v1 OUT ref_cursor, persid in varchar2) as
begin
open v1 for
SELECT lrn_exp_id, crs_name FROM emp_crs
WHERE personid = persid;
end;
Here ref_cursor is of TYPE REF CURSOR declared in the package specification.
The java code is ?
Callable stmt = conn.prepareCall("call sp_user_course2(?,?)");
stmt.registerOutParameter(1,OracleTypes.CURSOR);
stmt.setString(2,emplId);
stmt.execute();
OracleCallableStatement tstmt = (OracleCallableStatement) stmt;
ResultSet rs = tstmt.getCursor (1);
When I run the program, I get the following error (at stmt.execute()):
[Oracle][ODBC][Ora]ORA-06553: PLS-306: wrong number or types of arguments in call to 'SP_USER_COURSE2' 6553
Can anyone tell me what could be the problem with this code? I have read somewhere that REF CURSOR feature is available from Oracle 9i onwards. If so, then, is there any workaround for mapping REF CURSOR to Resultsets in Oracle 8i?These may help
http://www.google.co.uk/search?q=jdbc+OracleTypes.CURSOR+tutorial -
Calling Stored Proc returning count of records(int value) using hibernate
Hi ,
I am a newbie in hibernate. i have a store proc that returns the count of amtchin records, i want to call the store proc using hibernat.Can anyone help me out in this.
Thanks ,
Diliphttp://asktom.oracle.com/pls/ask/f?p=4950:8:6003223320663969737::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:246014735810,
-
Use 'default' keyword in call string while calling stored proc?
I am calling following sql server stored procedure from java code using my jdbc driver for sql server: CREATE PROCEDURE test_findTbInfo (
@paramIn_Str varchar(10),
@paramOut_Int int OUT,
@paramIn_Int int = 20
AS
begin
set @paramOut_Int = @paramIn_Int * 100
end
If I make a call like this:
CallableStatement cs = conn.prepareCall(" { call test_findTbInfo(? , , ? ) }");
cs.setString(1, "test_tab");
cs.setInt(2, 4);
cs.execute();
It works without any error. But this is not a right behavior. !! The second parameter as you see is passed like an optional parameter. But in stored proc it is NOT an optional param and so if the value not passed it should fail...
Now if I change the code to
CallableStatement cs = conn.prepareCall(" { call test_findTbInfo(? , default, ? ) }");
it works correctly. Gives error that "Procedure 'test_findTbInfo' expects parameter '@paramOut_Int', which was not supplied." which is correct.
So is it a normal practice to use 'default' keyword while calling sql server stored procedures having optional parameters in jdbc ????
Anyone knows ??? As far as I know "call test_findTbInfo(? , , ? )" works fine except in some cases and also it forces users to put all optional parameters at the end of parameter list in stored proc.
Please let me know whether I should go with 'default' throuout for sql server stored proc while calling using my jdbc driver.
Amit{?= call <procedure-name>[<arg1>,<arg2>, ...]}The question mark in the above is the result parameter
that must be registered.
That is not the same as an OUT argument.Yes that is true. The result value and OUT parameters are different but they both MUST be registered as per jdbc API
"The type of all OUT parameters must be registered prior to executing the stored procedure; their values are retrieved after execution via the get methods provided here."
Anyway, my original question still stays as it was. If there are some optional IN parameters in stored procedure
e.g.
PROCEDURE test_findTbInfo (
@paramIn_Int int = 20,
@paramOut_Int int OUT
how do you call this?
CallableStatement cs = conn.prepareCall(" { call test_findTbInfo( , ? ) }");
cs.registerOutParameter(1, Types.INTEGER);
cs.execute();
or
CallableStatement cs = conn.prepareCall(" { call test_findTbInfo(default, ? ) }");
Also note that I am intending to use ONLY sql server driver for this.
The first as well second seem to work. Except that second way is seems reliable. I just wanted a second opinion on that...
Amit -
Calling stored proc via services
I would be grateful if you could help me in resolving a technical issue in CMS(stellent).
We have a requirement that we need to call a stored procedure through services .The stored proc takes an input argument and returns a result set and has a defination that starts like
create or replace
PACKAGE BODY GETPROJECTPCKG IS
PROCEDURE PROC_RPU (rpu_list_count IN number, temp_project_cursor OUT project_ref_cursor) IS.
I've defined a service call like
<tr>
<td>GET_RPU_INFO</td>
<td>DocService
33
RECENTPROJECTUPDATE
null
null<br>
null</td>
<td>5:QgetRpuNames:temp_project_cursor::null</td>
</tr>
And below is how I've called the procedure
<tr>
<td>QgetRpuNames</td>
<td>{call GETPROJECTPCKG.PROC_RPU(?)}</td>
<td>rpu_list_count int</td>
</tr>
i tried calling procedure as below also ..
<tr>
<td>QgetRpuNames</td>
<td>{call GETPROJECTPCKG.PROC_RPU(?,?)}</td>
<td>rpu_list_count int
temp_project_cursor out:resultset</td>
</tr>
As per my understanding the temp_project_cursor which is the resultset that would be returned from procedure should be available on the template specified.
But while executing the service I am getting an error :
Unable to create result set for query 'QgetRpuNames({call GETPROJECTPCKG.PROC_RPU(10)})'. ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'PROC_RPU'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored)
Can you please assist.Hey
if this is still an issue please refert "Call Stored Procedure from Oracle Fusion ECM (Stellent) " at http://www.corecontentonly.com/index.php/downloads/. Jason has shared a component that you can make use of.
cheers,
sapan -
How to use stored procedure which returns result set in OBIEE
Hi,
I hav one stored procedure (one parameter) which returns a result set. Can we use this stored procedure in OBIEE? If so, how we hav to use.
I know we hav the Evaluate function but not sure whether I can use for my SP which returns result set. Is there any other way where I can use my SP?
Pls help me in solving this.
ThanksHi Radha,
If you want to cache the results in the Oracle BI Server, you should check that option. When you run a query the Oracle BI Server will get its results from the cache, based on the persistence time you define. If the cache is expired, the Oracle BI Server will go to the database to get the results.
If you want to use caching, you should enable caching in the nqsconfig.ini file.
Cheers,
Daan Bakboord -
Calling stored proc from java using ref cursor
Hi All,
We have a requirement to display all the records from a table on a JAVA screen. Due to some constraints, we have to write the query in the stored proc. We are trying to return a result set from the stored proc to the java code by using a ref cursor. Please refer to the code below.
Following is the PL/SQL proc �
procedure sp_user_course2(v1 OUT ref_cursor, persid in varchar2) as
begin
open v1 for
SELECT lrn_exp_id, crs_name FROM emp_crs
WHERE personid = persid;
end;
Here ref_cursor is of TYPE REF CURSOR declared in the package specification.
The java code is �
Callable stmt = conn.prepareCall("call sp_user_course2(?,?)");
stmt.registerOutParameter(1,OracleTypes.CURSOR);
stmt.setString(2,emplId);
stmt.execute();
OracleCallableStatement tstmt = (OracleCallableStatement) stmt;
ResultSet rs = tstmt.getCursor (1);
When I run the program, I get the following error (at stmt.execute()):
[Oracle][ODBC][Ora]ORA-06553: PLS-306: wrong number or types of arguments in call to 'SP_USER_COURSE2' 6553
Can anyone tell me what could be the problem with this code? I have read somewhere that REF CURSOR feature is available from Oracle 9i onwards. If so, then, is there any workaround for mapping REF CURSOR to Resultsets in Oracle 8i?These may help
http://www.google.co.uk/search?q=jdbc+OracleTypes.CURSOR+tutorial
Maybe you are looking for
-
Hi all, THERE IS PO WTIH FOLLOWING SCENARIO Amount(inr) PO 100 + 16.48 ( TAXES-EXCISE ) = 116.48 GR 100 + 12.00 ( TAXES-EXCISE ) = 112 while doing MIRO system should pick the taxes from MIGO.but it is picking from PO. when we d
-
Image in paragraph style?
I use CS3 - Windows XP Hello I am publishing a photobook in several languages, and before I start description of each photograph I need to put in the flag of the corresponding language. Kind of like a drop cap. I was wondering if there is some way to
-
How do I get to Chat with someone from Adobe?
How do I get to Chat with a real, live person so that I can ask my question about where to buy Captivate!
-
Used Migration Assistant to successfully transfer PC files to Mini. However file creation or modified date does not transfer. Date of creation is listed as day of transfer. I would gratefully appreciate any help in solving this?
-
How do you transport a Infoset / Ad Hoc Query to diff. clients?
HI, I created an infoset in development using sq02, now I want to transport it to Quality. But when I save the inofset, it does not create a transport request, but it just saves. I also see an option of transport in t-code sq 02 (SAP Query: Transport