I have written a Stored Procedure as shown , which will be responsible to give the
list of the EMployee Numbers
create or replace procedure
kiran
as
CURSOR cur is select * from emp;
TEMPCUR cur%rowtype;
begin
open cur;
loop
fetch cur into TEMPCUR;
DBMS_OUTPUT.PUT_LINE(TEMPCUR.empno);
exit when cur%NOTFOUND;
end loop;
close cur;
end;
/
Please tell me how can i retrive this values from a JDBC program
callableStatement = dbConnection.prepareCall(kiran);
// Please help need logic here
You cannot do it with exactly the procedure you listed, but you can easily do it if you change your procedure to return ref cursor. There are numerous examples of doing that. Here's the first one from Google search results: http://www.enterprisedt.com/publications/oracle/result_set.html .
EDIT:
Based on the changed stored procedure definition, the JDBC command would look like this:
{? = call test}
You will have to create a CallableStatement with this command, register an out parameter of type Oracle REF CURSOR and execute it. You will get your results as the first output parameter:
ResultSet rs = (ResultSet)stmt.getObject(1);
Here's how you do it. Remember, the last parameter in my example is actually declared in the PL/SQL procedure as an "OUT" parameter, e.g. the value you want to return in the form of a parameter):
conn --- is the connection object
cstmt --- this is a callablestatement
cstmt = conn.getNewCallableStatement("{call MY_PLSQL_PACKAGE.MY_PROC(?,?,?)}");
cstmt.setString(1, stringOutput1);
cstmt.setString(2, stringOutput2);
cstmt.registerOutParameter(3, OracleTypes.CURSOR);
cstmt.execute;
Related
A query can be returned in a "collection query" mode as a JSON, simply as SELECT * FROM SOMETABLE.
In PL/SQL however, this is not possible. How would the equivalent be implemented in this mode?
Easiest way is to return a refcursor from your procedure, as a RESULTSET parameter type.
For example:
DECLARE
cur SYS_REFCURSOR;
BEGIN
OPEN cur FOR
SELECT * FROM myTable ORDER BY id;
:result := cur;
END;
With your OUT parameter set to bind result.
Example sample schema, data, and REST Code here - For a longer full example.
I am trying to call procedure in oracle apex but I am facing problem in displaying the output of procedure while passing the parameter as emp_id to that procedure from oracle apex. Can anyone help me?
procedure that I have written in SQL developer tool.
create or replace PROCEDURE TEST_PROC(EMP_ID1 IN Number)
As
RESULT TIMESHEET_EMPLOYEES%ROWTYPE;
BEGIN
SELECT * INTO RESULT.EMP_ID,RESULT.NAME,RESULT.LOCATION,RESULT.CITY,RESULT.COUNTRY,RESULT.EMPLOYMENT_TYPE,RESULT.EMAIL_ID,RESULT.PHONE_NUMBER,RESULT.CREATED_BY,RESULT.CREATED_ON,RESULT.UPDATED_ON,
RESULT.UPDATED_BY,RESULT.DATE_OF_JOINING,RESULT.ROLE_ID,RESULT.SUPERVISOR_ID FROM TIMESHEET_EMPLOYEES
WHERE EMP_ID=EMP_ID1;
DBMS_OUTPUT.PUT_LINE('EMP_ID:'||RESULT.EMP_ID||' '||'NAME:'||RESULT.NAME||' '||'LOCATION:'||RESULT.LOCATION||' '||'CITY:'||RESULT.CITY ||' '||'COUNTRY:'||RESULT.COUNTRY||' '||'EMPLOYMENT_TYPE:'||
RESULT.EMPLOYMENT_TYPE||' '||'EMAIL_ID:'||RESULT.EMAIL_ID||' '||'PHONE_NUMBER:'||RESULT.PHONE_NUMBER||' '||'CREATED_BY:'||RESULT.CREATED_BY||' '||'CREATED_ON:'||RESULT.CREATED_ON||' '||'UPDATED_ON:'||
RESULT.UPDATED_ON||' '||'UPDATED_BY:'||RESULT.UPDATED_BY||' '||'DATE_OF_JOINING:'||RESULT.DATE_OF_JOINING||' '||'ROLE_ID:'||RESULT.ROLE_ID||' '||'SUPERVISOR_ID:'||RESULT.SUPERVISOR_ID);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('SQLCODE'||' '||SQLCODE);
DBMS_OUTPUT.PUT_LINE('SQLERRM'||' '||SQLERRM);
END;
herein oracle apex in PLSQL code I am trying to call that procedure but I am not able to see any output.
BEGIN
TEST_PROC(:P1_EMPID);
END;
Can someone help me as I am new to APEX???
Apex can't display result of DBMS_OUTPUT.PUT_LINE.
Two simple options you might use:
add another, OUT parameter(s) to your current procedure, or
convert this procedure to a function which returns the result as a concatenated string
Both of them should return the result into an item on the page.
For example, if it were a procedure:
create or replace PROCEDURE TEST_PROC(EMP_ID1 IN Number,
par_result out varchar2
)
is
-- ... your current code goes here; I presume it does what you wanted
-- at the end, return the result as
par_result := 'EMP_ID:'||RESULT.EMP_ID||' '||'NAME:'||RESULT.NAME || ...
end;
Call it in a process as
TEST_PROC(:P1_EMPID, :P1_RESULT);
As of code you wrote: if you declared result as %rowtype, it is simpler to insert directly into it, not into every separate piece of it. For example:
select *
into result --> this
from timesheet_employees
where emp_id = emp_id1;
I have not found anywhere on how to call an Oracle stored procedure using ADODB from Excel where the stored procedure has no input parameters.
Fake example to illustrate lack of input parameters:
CREATE OR REPLACE PROCEDURE Get_Data
(OUTPUT OUT SYS_REFCURSOR) IS
******************************************************************************/
BEGIN
OPEN OUTPUT FOR
SELECT DISTINCT
B.ITEM_ID
B.ITEM_DESC
FROM ITEM_FILE B
WHERE ITEM_ID IS NOT NULL
ORDER BY B.ITEM_ID
;
END Get_Data;
/
Oh, and the stored procedure is required because we don't want to give users SQL access to create whatever SQL they want.
Is this even possible? And if so, what kind of code would it take to call it?
Thanks,
Dan
Transform your procedure to a function:
CREATE OR REPLACE FUNCTION Get_Data RETURN SYS_REFCURSOR IS
res SYS_REFCURSOR;
BEGIN
OPEN OUTPUT FOR
SELECT DISTINCT
B.ITEM_ID
B.ITEM_DESC
FROM ITEM_FILE B
WHERE ITEM_ID IS NOT NULL
ORDER BY B.ITEM_ID;
RETURN res;
END Get_Data;
/
Call in VBA would be as this:
cmd.CommandText = "{CALL Get_Data()}"
cmd.Properties("PLSQLRSet") = True
Set Rst1 = cmd.Execute
cmd.Properties("PLSQLRSet") = False
Note, by default an OUT parameters are used like this:
cmd.Parameters.Append cmd.CreateParameter("OUTPUT", adVarChar, adParamOutput, 100)
However, for RefCursor parameters you must not declare them with cmd.Parameters.Append.
Have a look at Provider for OLE DB Developer's Guide, it contains several examples. Chapter Stored Procedures and Functions Returning Rowsets should be the most relevant for you.
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)
So Im going trough an oracle database course and I have a homework where I have to create a procedure and "return" (I guess return) the resultset without using a refcursor, but all the examples I find make use of it.
So lets say I have this:
CREATE OR REPLACE PROCEDURE get_all_inventory() AS
BEGIN
SELECT * FROM Inventory;
END;
/
How do I make the procedure return the resultset without using refcursor?
is this even possible?
Thanks.
EDIT: If someone know a way of returning the result set in json that will be just awesome!
Aside from using JSON, you can also use collections as a return value. You have to create a package first for your procedure. Here's a sample code:
create OR REPLACE package get_all_inventory_package is
type arrayofrec is table of Inventory%rowtype index by pls_integer;
procedure get_all_inventory (o_return_variable OUT arrayofrec);
end;
/
create OR REPLACE package BODY get_all_inventory_package is
procedure get_all_inventory (o_return_variable OUT arrayofrec)is
return_variable arrayofrec;
begin
select * bulk collect into o_return_variable from Inventory;
end;
END;
/
declare
v_receiver get_all_inventory_package.arrayofrec;
begin
get_all_inventory_package.get_all_inventory(v_receiver);
for a in 1..v_receiver.count loop
dbms_output.put_line(v_receiver(a).Inventory_column);
end loop;
end;