Not able to call procedure in Oracle APEX - oracle

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;

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;

PLSQL Procedure to return result set without a refcursor

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;

How to use One procedure output variable data into another procedure

I have one requirement in which I have to design two procedure.First procedure will generate one output variable value and then second procedure will use to do its task. I am giving same kind of scenario in below code
create procedure existingProc(
begin
insert statementprogramming statement
);
create procedure MyProc(
begin
call existingProc();
-- Exsisting procedure return some value
-- and this value is used in MyProc
commit;
);
In the above code existingProc is already there in the system and I can not change it. IN the procedure transaction is begin but not committed. This procedure generate one value as Output param and MyProc will used this value.
I want that after executing the existingProc, MyProc procedure should get the value, but it is not happening and it is giving null.
what should i do here, Please help me. I can not share the code that why giving scenario.
Does this help clarify how to use the output parameters from one procedure in another procedure?
create procedure ExistingProc(output1 out number) is
begin
insert into someTable values ('some values')
RETURNING someCol INTO output1;
end;
create procedure MyProc(args ....) is
Result1 number; --output from existingProc
begin
ExistingProc(result1);
-- Use Result1 as needed
Update stuff ...
where id = Resutl1;
commit;
end;

Oracle stored procedure to return a list of ids

I'm trying to convert an sql stored procedure to oracle. It's a very trivial procedure. It just returns a list of ids from a table, which is very easy to do in sql.
SQL
CREATE PROCEDURE [dbo].[tspInstalledLanguages]
AS
BEGIN
SELECT language_id from languages
END
I'm a complete novice when it comes to oracle, and I've learned that it's not as straight forward as this.
I've tried the following:
Oracle
CREATE OR REPLACE PROCEDURE tspInstalledLanguages
AS
BEGIN
SELECT LANGUAGE_ID FROM LANGUAGES;
END;
With no luck.
I get a message that it's expecting select into
It is.
Example:
DECLARE
v_authName author.author_last_name%type;
BEGIN
SELECT author_last_name
INTO v_authName
FROM author
WHERE author_key = 'A103';
dbms_output.put_line('Name: '||v_authName);
END;
/
So using your code:
CREATE OR REPLACE PROCEDURE tspInstalledLanguages
IS
v_language NUMBER;
BEGIN
SELECT LANGUAGE_ID
INTO v_language
FROM LANGUAGES;
dbms_output.put_line(v_language);
END;
/

Calling a stored PROCEDURE in Toad

I have a defined a new stored procedure but get a error while calling it,
CREATE OR REPLACE PROCEDURE SCOTT.getempsal(
p_emp_id IN NUMBER,
p_emp_month IN CHAR,
p_emp_sal OUT INTEGER)
AS
BEGIN
SELECT EMP_SAL
INTO p_emp_sal
FROM EMPLOYEE_SAL
WHERE EMP_ID = p_emp_id
AND EMP_MONTH = p_emp_month;
END getempsal;
And trying to call it:
getempsal(1,'JAN',OUT) --Invalid sql statement.
Your procedure contains an out parameter, so you need to call it in block like:
declare
a number;
begin
getempsal(1,'JAN',a);
dbms_output.put_line(a);
end;
A simple procedure (let's say with a number parameter) can be called with
exec proc(1);
or
begin
proc(1);
end;
Just write EXECUTE procedure_name('provide_the_valueof_IN parameter','value of in parameter', :k) ;
Run this statement a popup will come set the parameters as in out and the datatype too. U will see the output in another popup window.

Resources