Solved

How to return an OUT parameter from and ORACLE

Posted on 2016-11-15
3
36 Views
Last Modified: 2016-11-16
I need to find out how to return an OUT parameter from an ORACLE stored procedure. Below is a very very small and simple stored procedure with an output parameter named auditFlag.

Somehow I also need to set the value of auditingIndicator based upon the query being executed.

So, how can I set the value of auditingIndicator  based upon the query being executed at line 9? And how do I return the value using
my OUT parameter : auditFlag ?

1 CREATE PROCEDURE PROCESS_TRACKER (
2   tableName IN VARCHAR := NULL,
3   auditFlag IN OUT CHAR)
4 AS
5   queryString VARCHAR2(5000);
6   auditingIndicator INT;

7  BEGIN
8   auditingIndicator := 0;
9   queryString := 'SELECT auditValue FROM ' || tableName || ' WHERE isMarked = 1';
10   EXECUTE IMMEDIATE queryString;
   -- Somehow set "auditingIndicator" based on executing the previous query string
11    IF auditingIndicator = 1
12     THEN
      -- Do something
 13   END IF;


  END;
0
Comment
Question by:brgdotnet
  • 2
3 Comments
 
LVL 28

Accepted Solution

by:
Naveen Kumar earned 250 total points
ID: 41889235
Hi,

you cannot select without an into clause in the select statement in your execute immediate query. Please correct it.

Assuming that you have corrected this, based on the returned value from the column, you can do the below if condition to assign value to your out variable.

IF auditingIndicator_variable = 1
   THEN
      auditFlag := 'Y';
else
auditFlag := 'N';
 END IF;

Thanks,
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 41889242
also i would suggest addition exception handlers to your code block so you can  trap no data found, too may rows, etc. That would be helpful for you to understand if any errors/issues to easily debug/troubleshoot code.

Thanks,
0
 
LVL 20

Assisted Solution

by:flow01
flow01 earned 250 total points
ID: 41889497
first follow the posts above
1)
returning the out   parameter

declare
   v_auditFlag  varchar2(10);
begin
   PROCESS_TRACKER ('your table_name',v_auditFlag);
   dbms_output.put_line('The value of the auditFlag:' || v_auditFlag);
end;

2)
if the procedure has only 1 out  parameter   you also can change it to a function
CREATE FUNCTION PROCESS_TRACKER (  tableName IN VARCHAR) RETURN varchar2
IS
  v_auditFlag  varchar2(10);
BEGIN
   ...
   RETURN v_auditFlag;
END;

-- then getting the value gets
begin
      dbms_output.put_line('The value of the auditFlag:' || PROCESS_TRACKER ('your table_name'));
end;

3
a select into  expects only 1 row to be found :  if 2 rows are found you wil get an error
You really get 1 row  when executing  SELECT auditValue FROM  yourtableName  WHERE isMarked = 1?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now