oracle 是否可以在存储过程中使用“返回”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13225532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to use "return" in stored procedure?
提问by user1
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;
- Is it possible to use return in stored procedure like above?
- If we can use return, how can i get that return value in
Executesql("begin Pname(----)END")
method
- 是否可以像上面那样在存储过程中使用 return ?
- 如果我们可以使用 return,我如何在
Executesql("begin Pname(----)END")
方法中获得该返回值
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;
回答by Yogendra Singh
In Stored procedure, you return the values using OUT
parameter ONLY. As you have defined two variables in your example:
在存储过程中,您使用OUT
参数ONLY返回值。正如您在示例中定义了两个变量:
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.
只需将返回值分配给输出参数outstaticip
,outcount
然后从调用位置访问它们。我的意思是:当你调用存储过程时,你也会传递这两个变量。在存储过程调用之后,变量将填充有返回值。
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.
如果您希望RETURN value
从 PL/SQL 调用返回,请使用FUNCTION
. 请注意,万一您只能返回一个变量作为返回变量。
回答by Vaibhav Desai
Use FUNCTION:
使用功能:
CREATE OR REPLACE FUNCTION test_function
RETURN VARCHAR2 IS
BEGIN
RETURN 'This is being returned from a function';
END test_function;
回答by user_sk
-- 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.
-- 可以从变量 x 和 y(ans1:= x 和 ans2:=y)收集的过程输出分别为:150 和 20。
-- Answer borrowed from: https://stackoverflow.com/a/9484228/1661078
回答by Manisundaram R
It is possible.
有可能的。
When you use Return inside a procedure, the control is transferred to the calling program which calls the procedure. It is like an exit in loops.
当您在过程中使用 Return 时,控制权将转移到调用该过程的调用程序。这就像循环中的退出。
It won't return any value.
它不会返回任何值。
回答by arjun sah
CREATE PROCEDURE pr_emp(dept_id IN NUMBER,vv_ename out varchar2 )
AS
v_ename emp%rowtype;
CURSOR c_emp IS
SELECT ename
FROM emp where deptno=dept_id;
BEGIN
OPEN c;
loop
FETCH c_emp INTO v_ename;
return v_ename;
vv_ename := v_ename
exit when c_emp%notfound;
end loop;
CLOSE c_emp;
END pr_emp;