yes that exactly what I'm thinking about, I'll just take a look at the link and tehn return.
By they way, is that not the best way to do it?
Main Topics
Browse All Topicscan I access a PL/SLQ cursor from within a Java program (On the middle-tier)?
If so can someone please show me a simple example?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Thanks
I'm relatively new to to REF CURSOR (well pl/sql in generel), but I was wondering what to return when using REF CURSOR. As an example this wouldn't work, since I need to return a REF cusor and not NULL:
EXCEPTION
WHEN NO_DATA_FOUND
THEN
RETURN 'NO DATA FOUND';
WHEN TOO_MANY_ROWS
THEN
RETURN NULL;
WHEN OTHERS
THEN
RETURN NULL;
I have tested the java code with my REF CURSOR function (without exception handling within PL/SQL). The exception handling is created within java and it works fine, but I would also like to be able to do it in PL/SQL.
When I call the PL/SQL function with an invalid value I receive "ORA-01722: invalid number", just as i'm suppose to. But I think it would be best if I was able to do the exception handling from within PL/SQL since it would increase reusability.
Since the code stub you provided has a RETURN, I'm assuming a function and not a procedure.
You should be able to return null. I'm not sure why this isn't working. If you can provide a little more detail I'll attempt to assist you.
Since this is getting a little more complicated, I'll unaccept the answer to this question and let other experts assist.
CREATE OR REPLACE PACKAGE MHCI_poTypes
IS
TYPE ref_cursor IS REF CURSOR;
FUNCTION MHCI_PO_LINES(
paramHEADER_ID VARCHAR, paramPROJECT_ID VARCHAR, paramTASK_ID VARCHAR) RETURN ref_cursor;
FUNCTION MHCI_PO_HEADERS(
paramHEADER_ID VARCHAR, paramPO_LINE_ID VARCHAR) RETURN ref_cursor;
END MHCI_poTypes;
CREATE OR REPLACE package body MHCI_poTypes
IS
FUNCTION MHCI_PO_LINES(
paramHEADER_ID VARCHAR, paramPROJECT_ID VARCHAR, paramTASK_ID VARCHAR) RETURN ref_cursor
IS
csr_poLines_ref ref_cursor;
BEGIN
OPEN csr_poLines_ref FOR
SELECT
PAT.TASK_NUMBER TASK
,POL.LINE_NUM DOC
,'Missing' CP_Agr
,'Missing' STATUS
,POL.CREATION_DATE
,(POLA.UNIT_PRICE * POLA.QUANTITY) REVISION_AMT
,((POLL.QUANTITY - POLL.QUANTITY_CANCELLED) * POL.UNIT_PRICE) TOTAL_AMT
,(POLL.QUANTITY_RECEIVED * POL.UNIT_PRICE) RECIEVED_AMT
,(POLL.QUANTITY_BILLED * POL.UNIT_PRICE) INVOICE_AMOUNT
,(POLL.QUANTITY_CANCELLED * POL.UNIT_PRICE) CANCELLED_AMT
FROM
PA_TASKS PAT
,PO_LINES_ALL POL
,PO_LINE_LOCATIONS_ALL POLL
,po_lines_archive_ALL POLA
WHERE
POLA.PO_HEADER_ID = POL.PO_HEADER_ID
AND POLA.PO_LINE_ID = POL.PO_LINE_ID
AND POLL.PO_LINE_ID = POL.PO_LINE_ID
AND POL.PO_HEADER_ID = paramHEADER_ID
AND PAT.PROJECT_ID = paramPROJECT_ID
AND PAT.TASK_ID = paramTASK_ID;
RETURN csr_poLines_ref;
END MHCI_PO_LINES;
FUNCTION MHCI_PO_HEADERS(
paramHEADER_ID VARCHAR, paramPO_LINE_ID VARCHAR) RETURN ref_cursor
IS
csr_poHeader_ref ref_cursor;
BEGIN
OPEN csr_poHeader_ref FOR
SELECT
'Missing' BUYER
,POH.SEGMENT1 DOC
,POH.AUTHORIZATION_STATUS STATUS
,POH.CREATION_DATE CREATION_DATE -- DD-MM-YYYY HH:MM:SS
,'Missing' REVISION_AMT
,((POLL.QUANTITY - POLL.QUANTITY_CANCELLED) * POL.UNIT_PRICE) TOTAL_AMT
,(POLL.QUANTITY_RECEIVED * POL.UNIT_PRICE) RECIEVED_AMT
,(POLL.QUANTITY_BILLED * POL.UNIT_PRICE) INVOICE_AMOUNT
,(POLL.QUANTITY_CANCELLED * POL.UNIT_PRICE) CANCELLED_AMT
FROM
PO_HEADERS_ALL POH,
PO_DISTRIBUTIONS_ALL POD,
PO_LINES_ALL POL,
PO_LINES_ARCHIVE_ALL POLA,
PO_LINE_LOCATIONS_ALL POLL
WHERE
poh.po_header_id = pol.po_header_id
AND pol.po_line_id = poll.po_line_id
AND poll.po_line_id = pod.po_line_id
AND POH.PO_HEADER_ID = paramHEADER_ID
AND POLA.PO_LINE_ID = paramPO_LINE_ID;
RETURN csr_poHeader_ref;
END MHCI_PO_HEADERS;
END MHCI_poTypes;
I would like my java program to be able to receive a meningfull error message from a PL/SQL function.
I only been using exception handling in simple PL/SQL functions using static cursors, howevery I don't know how to do it using REF CURSOR.
Two simple examples should do the trick,
1) One using a predefined exception like TOO_MANY_ROWS
2) One using a custom defined exceptoin
By the way I don't need any java code, just the PL/SQL code.
Thanks :-)
I think I'm starting to understand. I'm slow at times.
You can trap no_data_found and the custom 'Too many Rows' but the only way I can think of requires an additional execution of the statement.
See if this helps:
--------------------------
drop table tab1;
create table tab1 (
col1 number
);
insert into tab1 values('1');
insert into tab1 values('2');
insert into tab1 values('2');
commit;
create or replace function junk( someParameter varchar2 ) return sys_refcursor
is
myCur sys_refcursor;
junkNumber number;
maxRows number := 1;
begin
--perform some data checks
begin
junkNumber := to_number(someParameter);
exception
when others then
raise_application_error(-2
end;
--check for too many rows or NO rows
begin
select count(*) into junkNumber from tab1 where col1 = to_number(someParameter);
case
when junkNumber = 0 then
raise_application_error(-2
when junkNumber > maxRows then
raise_application_error(-2
else
null;
end case;
end;
open myCur for 'select sysdate from tab1 where col1 = to_number(:x)' using someParameter;
return myCur;
end;
/
show errors
-- no rows found
select junk('0') from dual;
-- too many rows found
select junk('2') from dual;
-- not a valid number
select junk('Y') from dual;
-- Just right.....
select junk('1') from dual;
Business Accounts
Answer for Membership
by: slightwvPosted on 2007-03-12 at 08:09:38ID: 18702538
Do you mean a ref cursor being returned from a stored procedure?
resource.c om/oracle- ref-cursor -in- java.h tml
Check out:
http://www.databasedesign-