How to see refcursor result/output in Oracle SQL Developer? [duplicate] - oracle

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Best way/tool to get the results from an oracle package procedure
Oracle SQL Developer: Show REFCURSOR Results in Grid?
I am new to Oracle SQL Developer. I am using Oracle SQL Developer Version 3.0.
I was trying to test my SP using the following query.
DECLARE
type output_cursor is ref cursor;
P_CURSOR output_cursor;
BEGIN
P_CURSOR := NULL;
myPackage.mySPTest ( P_NOTIFICATION_ID => 1975357,P_CURSOR => P_CURSOR) ;
END;
When I ran the above query in my Oracle SQL Developer, I am getting a message 'anonymus block completed' and its not showing any result.
Can anyone help me, how to see the result.
.

You can use a bind variable declared in SQL Developer to hold and show the results:
var r refcursor;
exec myPackage.mySPTest(P_NOTIFICATION_ID => 1975357, P_CURSOR => :r);
print r;
exec is shorthand for an anonymous block so this is equivalent to:
var r refcursor;
begin
myPackage.mySPTest(P_NOTIFICATION_ID => 1975357, P_CURSOR => :r);
end;
/
print r;
Unless P_CURSOR is declared as something unhelpful, maybe...

To view your cursor results you need to loop through your cursor and print values. You need to know column names for what your cursor is returning. You can do something like:
DECLARE
type output_cursor is ref cursor;
P_CURSOR output_cursor;
BEGIN
P_CURSOR := NULL;
DOCTORS_APP.get_reminders ( P_NOTIFICATION_ID => 1975357,P_CURSOR => P_CURSOR) ;
//replace Column1 and Column2 with actual column names
FOR CUR_VAL in P_CURSOR LOOP
DBMS_OUTPUT.PUT_LINE(CUR_VAL.Column1||' '||CUR_VAL.Column2);
END LOOP;
END;

Related

How to write an Oracle procedure with a select statement (Specifically on SQL Developer)?

I want to create a simple Oracle Stored procedure on SQL Developer that will return some records on a simple select query. I do not want to pass in any parameter, but I just want the Records to be returned back from the procedure into a result set -> a suitable variable.
I have been trying to use the following syntax :
create or replace PROCEDURE Getmarketdetails2(data OUT varchar2)
IS
BEGIN
SELECT *
into data
from dual;
END Getmarketdetails2;
But it gives me an error while I try to execute with the following exec statement -->
Declare a Varchar2;
exec Getmarketdetails2(a);
Error: PLS-00103: Encountered the symbol "end-of-file" when expecting "something else".
Cause: Usually a PL/SQL compilation error.
Appreciate if anyone can help me out of this long pending situation! I have tried enough to find a basic guide to create a simple Oracle stored procedure and execute it in SQL Developer, but none of them answer to the point!!
You want:
DECLARE
a VARCHAR2(4000); -- Give it a size
BEGIN -- Begin the anonymous PL/SQL block
Getmarketdetails2(a); -- Call the procedure
DBMS_OUTPUT.PUT_LINE( a ); -- Output the value
END; -- End the anonymous PL/SQL block
/ -- End the PL/SQL statement
or:
VARIABLE a VARCHAR2(4000); -- Create a bind variable
EXEC Getmarketdetails2(:a); -- Execute the procedure using the bind variable
PRINT a -- Print the bind variable
Assuming an up-to-date Oracle version, you can use dbms_sql.return_result()
create or replace PROCEDURE Getmarketdetails2
IS
c1 SYS_REFCURSOR;
BEGIN
OPEN c1 FOR
SELECT *
from dual;
DBMS_SQL.RETURN_RESULT(c1);
END Getmarketdetails2;
/
Then simply run
exec Getmarketdetails2
The only drawback is that SQL Developer only displays the result as text, not as a proper result grid.
This is how I return a cursor in Oracle
PROCEDURE GetAllData (P_CURSOR OUT SYS_REFCURSOR)
IS
BEGIN
OPEN P_CURSOR FOR
SELECT *
FROM TABLE ;
END GetAllData ;
Declare a Varchar2;
exec Getmarketdetails2(a);
Your procedure is ok;
Instead of above query, use below query to run sp:
Declare
a Varchar2(10);
Begin
Getmarketdetails2(a);
End;

how to call oracle procedure that has in out cursor

I have this procedure and I want to know how to execute it in PL/SQL. Can someone please help me?
create or replace PROCEDURE "P_TEST"
(
c$inchidere IN OUT softys.pk_utils.GenericCursor,
p_OLD IN empl.code%type,
p_NEW IN empl.code%type,
p_NXT IN empl.code%type,
p_ORGID IN empl.ORGID%type
)
is
begin
open c$inchidere for
select
...
end
Calling a procedure in PL/SQL is easy. The tricky part is reading the REF CURSOR. We need to fetch it into variables or a record type which match the projection of the executed query. That your code uses something called softys.pk_utils.GenericCursor makes me think you might have problems with that bit.
Anyway, this example presumes the result set returns a single numeric column. You will need to adjust the target variables to match the actual projection.
declare
rc sys_refcursor;
l_id number;
begin
P_TEST (
c$inchidere => rc,
p_OLD => 'whatever',
p_NEW => 'meh',
p_NXT => 'ABC',
p_ORGID => 123456 );
loop
fetch rc into l_id;
exit when rc%notfound;
end loop;
end;
Not sure why the ref cursor parameter is defined as IN OUT. As you open it immediately it only needs to be OUT.

Output array in Oracle stored procedure

I'm trying to get result of a table "empDetails" using simple stored proc in Oracle SQL developer. Below is the SP definition
create or replace
PROCEDURE TEST_ALL (all_Cursor OUT SYS_REFCURSOR ) AS
BEGIN
open all_Cursor for
select * from empDetails;
END TEST_ALL;
when I execute the stored proc, I cannot able to view the output in Output result tab.
But I can fetch the data through direct SQL query select * from empDetails;
Any clue ?
Use this way:
declare
a SYS_REFCURSOR;
v_emp_detls empDetails%rowtype;
begin
TEST_ALL (all_Cursor=>a );
loop
FETCH a INTO v_emp_detls;
EXIT WHEN a%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_emp_detls.col1||v_emp_detls.col2..and so on);
end loop;
end;
An alternative is to use the following:
VARIABLE rc REFCURSOR;
begin
test_all(:rc);
end;
/
PRINT rc;
which works in SQL*Plus and in most other GUIs that allow you to run the above as a script (e.g. SQL Developer, Toad, etc)

Output results of Oracle stored proc from SQL Developer

I'm trying to call an Oracle stored proc using SQL Developer. The proc outputs results using a sys_refcursor. I right click in the proc window which brings up the Run PL/SQL window. When I choose the proc I want it creates all the input params etc for me. Below is the code I'm using to try and loop through the sys_refcursor and output the results, but I'm getting an error on the 'v_rec v_Return%rowtype;' line :
ORA-06550: line 6 column 9:
PLS-00320: the declaration of the type of this expression is incomplete or malformed.
ORA-06550: line 6 column 9:
PL/SQL: Item ignored
vendor code 6550
I found the looping code on a couple of other websites and it seems to be the way to do it but it's not working for me no matter what I try. Another question - on the DBMS_OUTPUT.PUT_LINE('name = ' || v_rec.ADM) am I referencing the v_rec correctly i.e. is v_rec."column_name" the correct way??
I'm not that used to Oracle and have never used SQL plus. Any suggestions appreciated.
DECLARE
P_CAE_SEC_ID_N NUMBER;
P_PAGE_INDEX NUMBER;
P_PAGE_SIZE NUMBER;
v_Return sys_refcursor;
v_rec v_Return%rowtype;
BEGIN
P_CAE_SEC_ID_N := NULL;
P_PAGE_INDEX := 0;
P_PAGE_SIZE := 25;
CAE_FOF_SECURITY_PKG.GET_LIST_FOF_SECURITY(
P_CAE_SEC_ID_N => P_CAE_SEC_ID_N,
P_PAGE_INDEX => P_PAGE_INDEX,
P_PAGE_SIZE => P_PAGE_SIZE,
P_FOF_SEC_REFCUR => v_Return
);
-- Modify the code to output the variable
-- DBMS_OUTPUT.PUT_LINE('P_FOF_SEC_REFCUR = ');
loop
fetch v_Return into v_rec;
exit when v_Return%notfound;
DBMS_OUTPUT.PUT_LINE('name = ' || v_rec.ADM);
end loop;
END;
Your problem is here:
v_Return sys_refcursor;
v_rec v_Return%rowtype;
v_Return is a cursor variable and has no specific structure (list of columns), so v_Return%rowtype is not a valid record structure to declare v_rec. It is even possible for different calls to the procedure to return cursors with different structures.
You know what you are expecting the structure of the returned cursor to be (but Oracle doesn't) so you need to explicitly define the appropriate record structure e.g.
type t_row is record (empno number, ename varchar2(30));
v_rec t_row;
You need a strongly typed ref cursor to be able to define it as a %ROWTYPE.
Example here
#Tony Andrews thanks for this it gave me a better idea where I was going wrong. Still having problems though - here's a shortened version of my proc. It's a bit complex in that it's selecting all fields from a subquery and 2 other values:
open p_fof_sec_refcur for
SELECT *
FROM(
SELECT securities.*, rownum rnum, v_total_count
FROM
(
SELECT
CFS.CAE_SEC_ID,
CFS.FM_SEC_CODE,
...
FROM
CAEDBO.CAE_FOF_SECURITY CFS
INNER JOIN caedbo.CAE_DATA_SET_ELEMENT CDSE_STAT
ON (CDSE_STAT.DATA_SET_ELEMENT_ID = CFS.APPR_STATUS)
...
WHERE APPR_STATUS = NVL(p_appr_status, APPR_STATUS)
...
)securities
)
WHERE rnum between v_pgStart and v_pgEnd;
I explicitly defined the output structure as below to match the return fields from the proc but I'm still getting an error:
v_Return sys_refcursor;
type t_row is record (CAE_SEC_ID NUMBER,FM_SEC_CODE VARCHAR2(7),...rnum number, v_total_count number);
v_rec t_row;
The error I get is
ORA-06504: PL/SQL: Return types of Result Set variables or query do not match
ORA-06512: at line 45
I'm just wondering is the "rownum rnum, v_total_count" part tripping me up. I'm pretty sure I have all the other fields in the output structure correct as I copied them directly from the proc.

Oracle SQL Developer: how to view results from a ref cursor?

If I have a function which returns a reference cursor for a query, how can I view the result set of this in SQL Developer? Toad has a special tab for viewing the results of a reference cursor, this is the functionality I would like to find.
SET SERVEROUTPUT ON;
VARIABLE X REFCURSOR;
EXEC PROCEDURE_WITH_OUTPUT_SYS_REFCURSOR(:X);
PRINT X;
Double click the cursor fields in your result record. On the right side there is a "..." icon. Click this and you'll see the contents
Hi I know this was asked a while ago but I've just figured this out and it might help someone else. Not sure if this is exactly what you're looking for but this is how I call a stored proc and view the output in SQL Developer.
In SQL Developer when viewing the proc, right click and choose 'Run' or select Ctrl+F11 to bring up the Run PL/SQL window. This creates a template with the input and output params which you need to modify. To return the results of a sys_refcursor you then need to declare a row type that is exactly equivalent to the select stmt / sys_refcursor being returned by the proc. Below I declare "type t_row" which matches my output fields, then loop through the returned sys_refcursor. If t_row matches my sys_refcursor then it gets populated with each row of the sys_refcursor:
DECLARE
P_CAE_SEC_ID_N NUMBER;
P_FM_SEC_CODE_C VARCHAR2(200);
P_PAGE_INDEX NUMBER;
P_PAGE_SIZE NUMBER;
v_Return sys_refcursor;
type t_row is record (CAE_SEC_ID NUMBER,FM_SEC_CODE VARCHAR2(7),rownum number, v_total_count number);
v_rec t_row;
BEGIN
P_CAE_SEC_ID_N := NULL;
P_FM_SEC_CODE_C := NULL;
P_PAGE_INDEX := 0;
P_PAGE_SIZE := 25;
CAE_FOF_SECURITY_PKG.GET_LIST_FOF_SECURITY(
P_CAE_SEC_ID_N => P_CAE_SEC_ID_N,
P_FM_SEC_CODE_C => P_FM_SEC_CODE_C,
P_PAGE_INDEX => P_PAGE_INDEX,
P_PAGE_SIZE => P_PAGE_SIZE,
P_FOF_SEC_REFCUR => v_Return
);
-- Modify the code to output the variable
-- DBMS_OUTPUT.PUT_LINE('P_FOF_SEC_REFCUR = ');
loop
fetch v_Return into v_rec;
exit when v_Return%notfound;
DBMS_OUTPUT.PUT_LINE('sec_id = ' || v_rec.CAE_SEC_ID || 'sec code = ' ||v_rec.FM_SEC_CODE);
end loop;
END;
there are no way to display a refcursor in datagrid in sqldeveloper.
we can define a refcursor,call SP,then print refcursor,then data will be printed in Script output window in a plane text mode,but not in Query Result window.

Resources