Output array in Oracle stored procedure - oracle

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)

Related

How to call a stored procedure in pl/sql developer

I am new at this and have a simple question.
I have created a procedure like so in pl/sql developer
CREATE OR REPLACE PROCEDURE myproc2 AS
BEGIN
SELECT cd_desc des, cd_value cd FROM v_codes WHERE cd_type='CVS02'
END;
Now I want to call the procedure and see the output however when I run this
BEGIN
myproc2;
END;
in Pl/sql I am getting an error saying object myproc2 is invalid
How do I call a stored procedure in PL/SQL?
You're calling it right, but the procedure is wrong. If you check its status, it is invalid.
In PL/SQL, a SELECT requires INTO:
CREATE OR REPLACE PROCEDURE myproc2 AS
l_cd_desc v_codes.cd_desc%type;
l_cd_value v_codes.cd_value%type;
BEGIN
SELECT v.cd_desc, v.cd_value
INTO l_cd_desc, l_cd_value
FROM v_codes v
WHERE v.cd_type = 'CVS02';
END;
Beware of possible NO_DATA_FOUND or TOO_MANY_ROWS exception.
Also, although it'll now run OK (I guess), you won't see anything because it is unknown what you'll do next. You could, for example, choose to display values you fetched. In that case, add
<snip>
WHERE v.cd_type = 'CVS02';
dbms_output.put_line(l_cd_desc ||', '|| l_cd_value);
END;
Don't forget to enable serveroutput.
As you commented, you got too_many_rows. How to handle it? It depends on what you want to do. One option is to switch to a cursor FOR loop; now you don't need local variables and - as there's no SELECT statement itself - no INTO clause either:
CREATE OR REPLACE PROCEDURE myproc2
AS
BEGIN
FOR cur_r IN (SELECT v.cd_desc, v.cd_value
FROM v_codes v
WHERE v.cd_type = 'CVS02')
LOOP
DBMS_OUTPUT.put_line (cur_r.cd_desc || ', ' || cur_r.cd_value);
END LOOP;
END;
One great thing about Oracle SQL Developer is the GUI and that it does things for you.
You can open a sheet and run it the traditional way:
BEGIN
PROCEDURENAME(PARAM);
END;
or you can use the GUI, find the object with the (View->) Find DB object, find it, click on it and use the green arrow in the toolbar. It will open a UI for any parameters you used within the procedure.
In SQL Developer, if you want to see the output then you can return a cursor:
CREATE OR REPLACE PROCEDURE myproc2(
o_cursor OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN o_cursor FOR
SELECT cd_desc AS des,
cd_value AS cd
FROM v_codes
WHERE cd_type='CVS02'; -- You need a ; statement terminator here.
END;
/
Then you can use:
-- Declare a cursor bind variable
VARIABLE cur SYS_REFCURSOR;
BEGIN
-- Call the cursor outputting into the bind variable.
myproc2(:cur);
END;
/
-- Print the cursor
PRINT :cur;
And run it as a script (using F5).

Executing vs Compiling a stored procedure

Hello I have some problems with dbms_output. I wrote this code and dbms output in SQL Developer doesn't work.
create or replace procedure imprimirNotEmpleat(no_nom varchar2)
as
nom varchar2(30);
cursor buscarnom is select nom_emp
from empleats
where nom_emp!=no_nom;
begin
open buscarnom;
fetch buscarnom into nom;
while buscarnom%found loop
dbms_output.put_line('Empleat: '||nom);
fetch buscarnom INTO nom;
end loop;
close buscarnom;
end;
You created the procedure, but never executed it. It just waits in the database.
But, before executing the procedure, run
set serveroutput on
in SQL Developer to enable output, and then
begin
imprimirNotEmpleat('ABC'); --> or whichever value it is
end;
/
Note that you won't see anything if cursor doesn't return any rows.

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;

In Oracle SQL developer how to see output if we run procedure from query browser

I am a little bit surprised when I compile and run procedure from the
green button as shown in pic1, I get the output from the output variable.
Here is my simple code:
CREATE OR REPLACE PROCEDURE RUNPROCEDURE
(P_para1 in EMP.ID%type,P_PARA out SYS_REFCURSOR)
AS
BEGIN
OPEN P_PARA FOR
SELECT *
FROM emp
WHERE ID = P_para1;
END RUNPROCEDURE;
But when the same I run from the query browser, I did not see any output :=
set serveroutput on;
declare P_PARA1 number;
P_PARA SYS_REFCURSOR;
begin
RUNPROCEDURE(
P_PARA1 => 2,
P_PARA => P_PARA
) ;
end;
/
It just displays
PL/SQL procedure successfully complete
and I don't see any output.
Any help appreciated.
The only output from this procedure is an output parameter cursor. SQL Developer provides a method of iterating this cursor semi-automatically, while SQL*Plus doesn't - but your procedure doesn't "return" those rows, it returns the cursor. Somewhere there has to be code to iterate through the cursor, fetch the rows, and display the content of those rows.

How do I return the rows from an Oracle Stored Procedure using SELECT?

I have a stored procedure which returns a ref cursor as follows:
CREATE OR REPLACE PROCEDURE AIRS.GET_LAB_REPORT (ReportCurTyp OUT sys_refcursor)
AS
v_report_cursor sys_refcursor;
report_record v_lab_report%ROWTYPE;
l_sql VARCHAR2 (2000);
BEGIN
l_sql := 'SELECT * FROM V_LAB_REPORT';
OPEN v_report_cursor FOR l_sql;
LOOP
FETCH v_report_cursor INTO report_record;
EXIT WHEN v_report_cursor%NOTFOUND;
END LOOP;
CLOSE v_report_cursor;
END;
I want to use the output from this stored procedure in another select statement like:
SELECT * FROM GET_LAB_REPORT()
but I can't seem to get my head around the syntax.
Any ideas?
Whenever I've had to do this; I've used the Oracle TYPE and CAST features.
Something like:
SELECT *
FROM TABLE(CAST(F$get_Cassette_Tracking('8029241') AS cass_tracking_tab_type))
You need to setup the TYPE and all the columns you need and have them use:
pipe ROW(out_obj)
to capture your data. There are many ways to do this and if I can dig out a better example I will but this might give you an idea.
See this SO for a working example: Oracle Parameters with IN statement?

Resources