Solved

How to return an OUT parameter from and ORACLE

Posted on 2016-11-15
3
53 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

932 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