Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use "return" in stored procedure?

 CREATE PROCEDURE Pname(in_Tid IN VARCHAR2,in_IP IN VARCHAR2,outstaticip OUT VARCHAR2,outcount OUT NUMBER)
 AS
 BEGIN
 select STATIC_IP into outstaticip from OP_TTER_MAPPING where TERMINAL_ID = in_Tid;
 if in_IP = outstaticip then
 return 1;
 else
 select COUNT(*) into outcount from OP_TTER_MAPPING where DYNAMIC_IP_LOW <= in_IP AND   DYNAMIC_IP_HIGH >= in_IP AND TERMINAL_ID = in_Tid;
 if outcount = 1 then
 return 1;
 else
 return 0;
  end if;
 end if;
 END;
  1. Is it possible to use return in stored procedure like above?
  2. If we can use return, how can i get that return value in Executesql("begin Pname(----)END") method

EDIT

Now I edited my return value in stored procedure like this, am I doing it right ?

CREATE PROCEDURE P_ValidateTIDIP(in_Tid IN VARCHAR2,in_IP IN VARCHAR2,outstaticip OUT VARCHAR2,outcount OUT NUMBER,outretvalue OUT NUMBER)
AS
BEGIN
select STATIC_IP into outstaticip from OP_TTER_MAPPING where TERMINAL_ID = in_Tid;
if in_IP = outstaticip then
    outretvalue:=1;
else 
    select COUNT(*) into outcount from OP_TTER_MAPPING where DYNAMIC_IP_LOW <= in_IP AND DYNAMIC_IP_HIGH >= in_IP AND TERMINAL_ID = in_Tid;
    if outcount = 1 then 
     outretvalue:=1;
    else
     outretvalue:=0;
   end if;
end if;
END;
like image 202
user1 Avatar asked Nov 05 '12 03:11

user1


3 Answers

In Stored procedure, you return the values using OUT parameter ONLY. As you have defined two variables in your example:

   outstaticip OUT VARCHAR2, outcount OUT NUMBER

Just assign the return values to the out parameters i.e. outstaticip and outcount and access them back from calling location. What I mean here is: when you call the stored procedure, you will be passing those two variables as well. After the stored procedure call, the variables will be populated with return values.

If you want to have RETURN value as return from the PL/SQL call, then use FUNCTION. Please note that in case, you would be able to return only one variable as return variable.

like image 119
Yogendra Singh Avatar answered Nov 18 '22 07:11

Yogendra Singh


Use FUNCTION:

CREATE OR REPLACE FUNCTION test_function
RETURN VARCHAR2 IS

BEGIN
  RETURN 'This is being returned from a function';
END test_function;
like image 6
Vaibhav Desai Avatar answered Nov 18 '22 08:11

Vaibhav Desai


-- IN arguments : you get them. You can modify them locally but caller won't see it
-- IN OUT arguments: initialized by caller, already have a value, you can modify them and the caller will see it
-- OUT arguments: they're reinitialized by the procedure, the caller will see the final value.
CREATE PROCEDURE f (p IN NUMBER, x IN OUT NUMBER, y OUT NUMBER)
IS
BEGIN
   x:=x * p;
   y:=4 * p;
END;
/

SET SERVEROUTPUT ON

declare
   foo number := 30;
   bar number := 0;
begin
   f(5,foo,bar);
   dbms_output.put_line(foo || ' ' || bar);
end;
/

-- Procedure output can be collected from variables x and y (ans1:= x and ans2:=y) will be: 150 and 20 respectively.

-- Answer borrowed from: https://stackoverflow.com/a/9484228/1661078

like image 5
user_sk Avatar answered Nov 18 '22 09:11

user_sk