如何将 oracle 过程的参数默认设置为选择的结果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7756777/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:22:27  来源:igfitidea点击:

How can I set an oracle procedure's parameter default to the result of a select?

oraclestored-proceduresprocedure

提问by JerryKur

I have an oracle Procedure and I want to set the default parameter to the result of a select statement such as below.

我有一个 oracle 过程,我想将默认参数设置为如下所示的 select 语句的结果。

procedure foo( startID number : = max(x.id) from xtable x )

but the code above is illegal syntax.

但上面的代码是非法的语法。

回答by Vincent Malgrat

You can use an otherwise unvalid value for this meaning, for example NULL:

您可以为此含义使用其他无效值,例如 NULL:

PROCEDURE foo (p_id NUMBER DEFAULT NULL) IS
   l_startID NUMBER := p_id;
BEGIN
   IF p_id IS NULL THEN
      SELECT max(id) INTO l_startID FROM xtable;
   END IF;
   ...
END;

If your function/procedure is in a package, you could also overloadit:

如果你的函数/过程在一个包中,你也可以重载它:

PROCEDURE foo (p_id NUMBER)
[...]

PROCEDURE foo IS
   l_startID NUMBER;
BEGIN
   SELECT max(id) INTO l_startID FROM xtable;
   foo(l_startID);
END;