Select All records from a table using SP in Oracle SQL Developer - oracle

I'm using SQL Oracle to build a stored procedure. I'm trying to build a stored procedure of the SQL-query below.And I want to return those data to a C# program.
select * from employee_master
I have tried following. Is this correct?
CREATE OR REPLACE PROCEDURE EMPLOYEE_SELECTALL (p_recordset OUTSYS_REFCURSOR)AS
BEGIN
OPEN p_recordset FOR
SELECT
*
FROM
EMPLOYEE_MASTER;
END EMPLOYEE_SELECTALL;

If you wish to build a stored procedure that return such resultset first of all you should check if you really need to do this. It's incidental and not recommended way for Oracle. But if you really need so, you should use REF CURSOR.

after executing your stored procedure in SQL Developer, it automatically brings back any output for you to view, including one or more ref cursors.
Example code and screenshots here

Related

View results of pl/sql stored procedure in Toad?

I'm new to Oracle, and I use Toad Data Point to create and test stored procedures.
I created this simple stored procedure:
CREATE OR REPLACE PROCEDURE dummy_sp (
p_recordset OUT SYS_REFCURSOR) AS
BEGIN
OPEN p_recordset FOR
select sysdate, user from dual;
END dummy_sp ;
/
I executed this, and the result from Toad is Executed Successfully.
Now, I would like to view the results of this stored procedure. In Toad Data Point I type the following:
variable mycursor refcursor;
call dummy_sp ( :mycursor );
I get a popup asking for a parameter. I click OK and I get the error ORA-00900: invalid SQL statement.
How can I see the result of SP dummy_sp in Toad Data Point?
In SQL Server I can run exec usp_sales and see the results of a select statement. There has to be something like that in Oracle and Toad, right?
Here you go, using Toad Data Point.
Execute the stored procedure with a bind variable in it, like :mycursor, and then make sure to configure the type as CURSOR and direction as OUT when Toad Data Point prompts you for the bind variable settings.
Here's the result:
Finally, if you wish to avoid the popup for bind variables, you can execute the procedure directly from the object explorer:
Right-click the procedure and choose Operations / Execute Procedure, and Toad will run it, without prompting for data type.
In case you need a workaround while you wait for help with your tool, the default, free IDE for Oracle Database makes this pretty easy.
If you execute the program using the code editor, it will automatically grab any outputs, whether those be OUT params or RETURNs from a function, including your refcursor
Or if your GUI has proper SQLPlus script execution support (SQL Developer does, not sure about your program):
var x refcursor
exec dummy_sp(:x);
print :x;
And the output:
PL/SQL procedure successfully completed.
SYSDATE USER
------------------- --------------------------------------------------------------------------------------------------------------------------------
27-JUN-19 13.58.47 HR

Stored Procedure in Oracle giving error PLS-00428

I am trying to create the following procedure in oracle:
CREATE OR REPLACE PROCEDURE SPBILL (SPCLIENT_ID VARCHAR2)
AS
BEGIN
SELECT C.CLIENT_NAME, B.ROOM_ID, R.ROOM_COST, T.TREAT_NAME, T.TREAT_COST, (ROOM_COST*(B_END_DATE-B_START_DATE)+TREAT_COST) AS INVOICE
FROM CLIENTS C, ROOMS R, TREATMENTS T, BOOKING B, PRESCRIPTION P
WHERE C.CLIENT_ID=B.CLIENT_ID
AND R.ROOM_ID=B.ROOM_ID
AND B.CLIENT_ID=P.CLIENT_ID
AND P.TREAT_ID=T.TREAT_ID
AND C.CLIENT_ID=SPCLIENT_ID;
END SPBILL;
I am getting a "Procedure created with compilation errors" and the errors is PLS-00428, which required an INTO satement, but i do not understand why and where do i need it as my sql statement works just the way i want it without the procedure. But i need to create a procedure so i can call a specific client id and only recieve their data as an output.
When you are running the SQL directly using a client (SQL Plus or SQL Developer or Toad) , data is returned to the client.
When you run the same query inside PL/SQL, you need to tell oracle what to do with that data. Usually programs store the output in Pl/SQL variables for further processing.
https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/selectinto_statement.htm
So, in your case, you might need something along these lines..
CREATE OR REPLACE PROCEDURE SPBILL (SPCLIENT_ID VARCHAR2)
AS
l_client_name clients.client_name%type;
l_room_id rooms.room_id%type;
...
l_invoice number(5,2);
BEGIN
SELECT C.CLIENT_NAME, B.ROOM_ID, R.ROOM_COST, T.TREAT_NAME, T.TREAT_COST, (ROOM_COST*(B_END_DATE-B_START_DATE)+TREAT_COST)
into l_client_name, l_room_id...l_invoice
FROM CLIENTS C, ROOMS R, TREATMENTS T, BOOKING B, PRESCRIPTION P
WHERE C.CLIENT_ID=B.CLIENT_ID
AND R.ROOM_ID=B.ROOM_ID
AND B.CLIENT_ID=P.CLIENT_ID
AND P.TREAT_ID=T.TREAT_ID
AND C.CLIENT_ID=SPCLIENT_ID;
--further processing here based on variables above.
dbms_output.put_line(l_invoice);
END SPBILL;
Once you compile without errors, you can run the procedure..
set serveroutput on;
SPBILL(100);
Maybe you should use IS instead of AS
CREATE OR REPLACE PROCEDURE SPBILL (SPCLIENT_ID VARCHAR2)
IS
....
It looks like you have experience with MSSQL and expect Oracle would be the same. If so, it isn't true.
It looks like you try to return resultset from procedure as it usual in MSSQL. Oracle have no implicit resultsets at all. If you wish to do this, you should use explicit resultset eighter via returning REF CURSOR (http://www.orafaq.com/wiki/REF_CURSOR) or via TABLE FUNCTION (https://docs.oracle.com/cd/B19306_01/appdev.102/b14289/dcitblfns.htm).
But first of all, you should think if you really need this procedure at all. Generally, SELECT procedures in Oracle is part of doubtful design.

Oracle procedure: options to return data

I'm used to Microsoft SQL Server, where the last SELECT query of a stored procedure determines what is returned.
In Oracle, I'm always using a SYS_REFCURSOR OUT parameter to return data from queries.
Are there other options of returning data from SELECT queries in Oracle stored procedures?
How about SELECT queries that select only one row? Is a SYS_REFCURSOR still necessary?
EDIT: I need to know the answer for Oracle 11g R2 (should have mentioned that explicitly instead of just in the tags).
Until now Oracle has not supported the SQL Server style of procedures implicitly returning a result set, you have had to explicitly return something - which could be a SYS_REFCURSOR or a collection or whatever.
In Oracle 12C a new feature has been added called Implicit Statement Results, which is designed to emulate the SQL Server way of working. However, this is really intended to support migration of existing code from SQL Server; for fresh Oracle development you would be best advised to learn the way Oracle normally does things.
Oracle PL/SQL Procedures can return all the supported basic datattype( date,varchar2,number) plus complex ( records, tables, varray)
Another option is the pipelined function, in which you call the function as:
select ...
from table(my_function(param1 => 1, ...))
See docs for details.
Just for clarifying, ORACLE procedures cannot 'RETURN" per se, a SYS_REFCURSOR OUT parameter is more like changing the value of a variable reference inside the procedure.
Apart from SYS_REFCURSOR, if you are returning only one row of a table, say EMPLOYEE, you can also define a record as EMPLOYEE%ROWTYPE and use it as a OUT type.
Or like:
PROCEDURE pr_proc (v_input in number
v_emp_row out EMPLOYEE%ROWTYPE )
IS

Simple Select inside an Oracle Stored Procedure

how do you create a stored procedure with a simple select (SELECT * FROM TABLE) using Oracle? Also, any good tutorials on stored procedures would help tremendously.
Thanks.
It depends on what you're trying to return from the stored procedure (resultset vs. scalar value) and what version of Oracle you're on (newer versions make this easier).
This question is probably a dupe of Get resultset from oracle stored procedure.
create or replace procedure spr_select_Emp(eno in number, employee out emp%RowType)
As
Begin
Select empno,ename,mgrno,hiredate,sal,comm,deptno into employee from emp
where empno=eno
End;
A procedure is created using the Oracle create or replace procedure syntax below:
create or replace procedure
()
as (or is)
local variable declaration
begin
code section
exceptions
end;
more info here: http://www.dba-oracle.com/t_create_or_replace_procedure.htm

Quick-n-dirty results: View results of Procedure OUT cursor in SQL Worksheet?

Platform: Oracle
Language: PL/SQL
Issue: Want to output a procedure OUT cursor into the SQLDeveloper SQLWosksheet.
Anyone know how to use the Oracle "Select * from Table( PipelinedFunction( Param ) ) " to check procedure code output cursors?
I am using Crsytal Reports off of an Oracle stored procedure. Crystal requires that a procedure return a cursor, which it fetchs and reads.
The procedure code I have is currently working, but I want to find the easiest way to view the effects of changes to the procedure code. I have SQLDeveloper available, and I'm doing my creation and sql testing in that. I would like to get a quick result visible in the SQL Developer Query Result window ("SQL Worksheet").
Is there a (simple) way to use a Function to read the cursor from the procedure? (and pipe that out to the Table function?)
Convoluted, I know, but I deal best when I can just see the results of code changes. If I can view the record results directly, it will speed up development of the report.
I know of the Table function and a little about pipelining in Oracle. I know a little about cursors in general and sys_refcursor. I know diddly about types and why I need them. (Isn't sys_regCursor supposed to get us away from that?)
The current procedure does an adequate but ungraceful series of queries, inserts to global temp tables (GTT), joins from GTT and original tables, more inserts, and more self-joins and then SELECTS the results into the OUT cursor. I might be able to do better relying on just cursors and such, but the current method is good enough to get results to the report.
I think I can handle SQL pretty well (for our purposes), but I am not an Oracle-specific developer... but I need help.
Anybody run across this? The whole idea was to speed my development for the procedure code, but I've spent a couple of days looking for a way to just get at the output... not what I had in mind.
Update:
I have tried some hare-brained schemes based on slivers that I've seen on the web... such as
Create or replace FUNCTION GET_BACKPLANE (
Node VARCHAR2 ) RETURN SYS_REFCURSOR
AS
RESULTS SYS_REFCURSOR;
BEGIN
Open Results for
Select Backplane(Results, Node) from Dual ;
... etc.
and
Create or replace Function GET_BACKPLANE (
NODE VARCHAR2 ) RETURN My_Table_Stru%ROWTYPE PIPELINED
AS
BEGIN ...
I don't think that Oracle is even considering letting me re-reference the output cursor from the procedure ("Results" is a sys_refcursor that holds the results of the last SELECT in the procedure). I don't know how to define it, open it, and reference it from the procedure.
I never got to the place where I could try
SELECT * FROM TABLE(GET_BACKPLANE( ... etc )
Sorry for any typos and bad Oracle Grammar... it's been a long several days.
SQL Developer allows us to use SQL*Plus commands in the Worksheet. So all you need to do is define a variable to hold the output of the ref cursor.
I may have misinterpreted the actual code you want to run but I'm assuming your actual program is a procedure Backplane(Results, Node) where results is an OUT parameter of datatype sys_refcursor and node is some input parameter.
var rc refcursor
exec Backplane(results=>:rc, Node=>42)
print rc
The output of the print statement is written to the Script Output pane.
Note that the use of SQL*Plus commands means we have to use the Run Script option F5 rather than execute statement.
Thanks for the help. In the end, I wound up brute-force-ing it...
Step by step:
Make a query, test a query,
create a global temp table from the structure,
add code to make another query off of that GTT, test the query,
create a global temp table from the structure,
etc.
In the end, I wound up running (anonymous block) scripts and checking the GTT contents at every stage.
The last part was to use the same last query from the original procedure, stuffing everything into the Cursor that crystal likes...
tomorrow, I test that.
But, I'll just force it through for the next procedure, and get it done in a day and a half instead of 2+ weeks (embarrassed).
Thanks,
Marc

Resources