是否可以在存储过程中使用"return"?

use*_*er1 10 oracle plsql stored-procedures oracle10g oracle11g

 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;
Run Code Online (Sandbox Code Playgroud)
  1. 是否可以像上面那样在存储过程中使用return?
  2. 如果我们可以使用return,我怎样才能在Executesql("begin Pname(----)END")方法中获得返回值

编辑

现在我在这样的存储过程中编辑了我的返回值,我做得对吗?

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;
Run Code Online (Sandbox Code Playgroud)

Yog*_*ngh 11

在存储过程中,使用OUT参数返回值.正如您在示例中定义了两个变量:

   outstaticip OUT VARCHAR2, outcount OUT NUMBER
Run Code Online (Sandbox Code Playgroud)

只需将返回值分配给out参数outstaticip,outcount然后从调用位置访问它们.我的意思是:当你调用存储过程时,你也会传递这两个变量.在存储过程调用之后,将使用返回值填充变量.

如果你想RETURN value从PL/SQL调用返回,那么使用FUNCTION.请注意,万一,您只能返回一个变量作为返回变量.


Vai*_*sai 6

使用功能:

CREATE OR REPLACE FUNCTION test_function
RETURN VARCHAR2 IS

BEGIN
  RETURN 'This is being returned from a function';
END test_function;
Run Code Online (Sandbox Code Playgroud)


小智 5

-- 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;
/
Run Code Online (Sandbox Code Playgroud)

-- 可以从变量 x 和 y(ans1:= x 和 ans2:=y)收集的过程输出分别为:150 和 20。

-- 答案来自:https : //stackoverflow.com/a/9484228/1661078