Oracle Stored Procedure not Returning Results - oracle

My Oracle Stored Procedure does not return any results. I have tested the query outside the SPROC and it seems to return values. I have a feeling it has something to do with how I'm passing the sample and test parameters - because if I hardcode the sampleNumber and testNumber parameters in the Stored Proc, the results are returned correctly. I have pasted the Procedure and the PLSQL Declaration. I am very new to writing Oracle Procedures and any help will be greatly appreciated. Thank you.
CREATE OR REPLACE PROCEDURE TESTANALYSIS
(vOrderNumber IN VARCHAR2
, vTestNumber IN NUMBER
, vSampleNumber IN NUMBER
)
AS
anVar VARCHAR2(50) := NULL;
BEGIN
SELECT T.ANALYSIS INTO anVar FROM TEST T JOIN SAMPLE S
ON T.SAMPLE_NUMBER = S.SAMPLE_NUMBER
WHERE T.TEST_NUMBER = vTestNumber
AND S.SAMPLE_NUMBER = vSampleNumber
AND S.ORDER_NUM = vOrderNumber;
dbms_output.put_line(anVar);
END TESTANALYSIS;
--PL SQL Call
SET SERVEROUTPUT ON
EXEC TESTANALYSIS('4200010061', 720000050516, 14789)
/

Related

Not able to call procedure in Oracle APEX

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;

Calling an oracle stored procedure with no parameters ADODB

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.

Update statement inside oracle stored procedure is not working

I have one simple update statement inside oracle stored procedure. Its executing successfully but its not updating the table.
CREATE OR REPLACE PROCEDURE UpdateSourceLog
( SourceLogId IN NUMBER, TotalRowCount IN INT,Status IN VARCHAR)
AS
BEGIN
UPDATE SourceLog
SET Status = Status,
TotalRowCount = TotalRowCount,
EndTime = SYSDATE
WHERE SourceLogId = SourceLogId;
COMMIT;
END;
I have tried with changing the perameter name different from column name. Then also Its not working.
And I have tried with anonymous block. I'm not able to find out the isue. Please help me in this regard.
Thanks!
It is a bad practice to give parameters the same name as table columns.
So you should change it:
CREATE OR REPLACE PROCEDURE UpdateSourceLog
( p_SourceLogId IN NUMBER, p_TotalRowCount IN INT,p_status IN VARCHAR)
AS
BEGIN
UPDATE SourceLog
SET Status = p_status,
TotalRowCount = p_TotalRowCount,
EndTime = SYSDATE
WHERE SourceLogId = p_SourceLogId;
COMMIT;
END;
Because for now, most likely, Oracle understands it as column names and just update column to value from this column (no sense at all)

Oracle equivalent of stored procedure that returns an inline table?

Example in T-SQL (SQL Server - taken from here):
CREATE PROC proc_authors
#au_lname VARCHAR(40)
AS
SELECT
au_id, au_fname, au_lname, city, state
FROM authors
WHERE au_lname = #au_lname
go
Is it possible in Oracle to create a stored procedure that returns an inline table (without declaring a type - like the above)? If not, what would be the closest alternative? i.e. declare inline type, then use it. The idea is to minimize number of DB permissions that are granted.
Please include sample code as part of your answer.
Reasoning behind using stored procedure vs function - we have legacy software that can only execute stored procedures, or raw queries. It appears that only stored procedures in there have support for parameterized execution, which is what we are after.
try this with ref cursor
PROCEDURE proc_get_tada(ip_user IN VARCHAR2,
op_error_code OUT NUMBER,
op_cursor OUT SYS_REFCURSOR,) AS
BEGIN
OPEN op_cursor FOR
SELECT * FROM your_table yt where yt.user = ip_user;
EXCEPTION
WHEN OTHERS THEN
op_error_code := -1;
END proc_get_tada;
you will get collection of all data from you table you can iterate in java or calling program.
Maybe you are searching for something like this:
create table author
(
au_id number,
au_name varchar2(100)
);
insert into author (au_id, au_name) values(1, 'ME');
create or replace function getAuthor(auName varchar2)
return author%rowtype
is
retval author%rowtype;
begin
select * into retval from author where au_name=auName;
return retval;
end;
declare
auth author%rowtype;
begin
auth := getAuthor('ME');
dbms_output.put_line(auth.au_id);
end;

Oracle stored procedure to return a result set

I'm totally new to Oracle, and I need to write a stored procedure that will return a result set. The stored procedure will be called from SQL Server using a linked server.
I know I can use a ref cursor to return a resultset from PL/SQL server, but I've also seen stored procedures returning sort of arrays (declared as Table of Number for example) and then return this with the result.
Which is the proper way to do this?
After some investigation on Oracle forums and my own testings...
A Ref Cursor is the appropriate way.
The associate arrays will not work properly using the oracle oledb provider.
I'm running Oracle XE in a Docker container and using their sample database.
Here's the stored procedure that takes a parameter and returns a set of records.
CREATE OR REPLACE PROCEDURE customer_orders(cust_id IN NUMBER, cur OUT SYS_REFCURSOR) IS
BEGIN
OPEN cur FOR
SELECT *
FROM orders
WHERE customer_id = cust_id;
end;
/
Here's the Perl that executes the procedure, passing the customer id as a parameter, and processing the returned results set.
use DBI;
use DBD::Oracle qw(:ora_types);
# get the database connection
$dbh = DBI->connect("dbi:Oracle:localhost/xepdb1", "ot", "Orcl1234") || die(DBI->errstr());
$sth = $dbh->prepare("BEGIN customer_orders(?, ?); END;");
$sth->bind_param(1, 49);
$sth->bind_param_inout(2, \$rsh, 0, {ora_type => ORA_RSET});
$sth->execute();
while ( #row = $rsh->fetchrow_array ){
foreach ( #row ){
print "$_\t";
};
print "\n";
}
$dbh->disconnect;
I hope this saves all who pass this way the time it took me to figure it out.
Luck.

Resources