oracle PL/SQL 中的 IN、OUT、IN OUT 参数到底是什么

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

What exactly are IN, OUT, IN OUT parameters in PL/SQL

oraclestored-proceduresplsqltoad

提问by Jules

I've looked up questions here as well as looking online and watching videos but I'm still confused exactly what IN, OUT is. The reason I'm asking is because I'm writing a procedure that will log an error based on the IN parameters in other procedures,

我已经在这里查找问题以及在线查找和观看视频,但我仍然对 IN,OUT 到底是什么感到困惑。我问的原因是因为我正在编写一个程序,该程序将根据其他程序中的 IN 参数记录错误,

Cheers!

干杯!

回答by sstan

The Oracle documentation heredoes a good job of explaining:

这里的 Oracle 文档很好地解释了:

The mode of a parameter indicates whether the parameter passes data to a procedure (IN), returns data from a procedure (OUT), or can do both (IN OUT).

参数的模式指示参数是将数据传递给过程 ( IN)、从过程返回数据 ( OUT) 还是两者都可以 ( IN OUT)。

And about OUTparameters specifically:

关于OUT参数,特别是:

... you cannot use it to pass a value to the procedure. Nor can you read its value inside the procedure, even after a value has been assigned to it.

...您不能使用它向过程传递值。您也不能在过程中读取它的值,即使在为它分配了值之后也是如此。

EDIT

编辑

Actually, though the information provided above is valid, I linked to a poor resource (SQL*Module for Ada Programmer's Guide).

实际上,虽然上面提供的信息是有效的,但我链接到了一个糟糕的资源(SQL*Module for Ada Programmer's Guide)。

A much better and more complete resource to better understand the 3 modes can be found here: Table 8-1 PL/SQL Subprogram Parameter Modes.

可以在此处找到更好、更完整的资源以更好地理解这 3 种模式:表 8-1 PL/SQL 子程序参数模式

INmode:

IN模式

  • Default mode

  • Passes a value to the subprogram.

  • Formal parameter acts like a constant: When the subprogram begins, its value is that of either its actual parameter or default value, and the subprogram cannot change this value.

  • Actual parameter can be a constant, initialized variable, literal, or expression.

  • Actual parameter is passed by reference.

  • 默认模式

  • 将值传递给子程序。

  • 形参就像一个常量:当子程序开始时,它的值要么是它的实际参数,要么是默认值,子程序不能改变这个值。

  • 实际参数可以是常量、初始化变量、文字或表达式。

  • 实际参数通过引用传递。

OUTmode:

OUT模式:

  • Must be specified.

  • Returns a value to the invoker.

  • Formal parameter is initialized to the default value of its type. The default value of the type is NULLexcept for a record type with a non-NULLdefault value.

  • When the subprogram begins, the formal parameter has its initial value regardless of the value of its actual parameter. Oracle recommends that the subprogram assign a value to the formal parameter.

  • If the default value of the formal parameter type is NULL, then the actual parameter must be a variable whose data type is not defined as NOT NULL.

  • By default, actual parameter is passed by value; if you specify NOCOPY, it might be passed by reference.

  • 必须指定。

  • 向调用者返回一个值。

  • 形参被初始化为其类型的默认值。类型的默认值是NULL具有非NULL默认值的记录类型除外。

  • 当子程序开始时,无论其实参的值如何,形参都有其初始值。Oracle 建议子程序为形参赋值。

  • 如果形参类型的默认值为NULL,则实参必须是数据类型未定义为 的变量NOT NULL

  • 默认情况下,实参按值传递;如果您指定NOCOPY,则它可能通过引用传递。

IN OUTmode:

IN OUT模式:

  • Must be specified.

  • Passes an initial value to the subprogram and returns an updated value to the invoker.

  • Formal parameter acts like an initialized variable: When the subprogram begins, its value is that of its actual parameter. Oracle recommends that the subprogram update its value.

  • Actual parameter must be a variable (typically, it is a string buffer or numeric accumulator).

  • By default, actual parameter is passed by value (in both directions); if you specify NOCOPY, it might be passed by reference.

  • 必须指定。

  • 将初始值传递给子程序并将更新的值返回给调用者。

  • 形参就像一个初始化变量:当子程序开始时,它的值是它的实参的值。Oracle 建议子程序更新其值。

  • 实际参数必须是变量(通常是字符串缓冲区或数字累加器)。

  • 默认情况下,实参按值传递(双向);如果您指定NOCOPY,则它可能通过引用传递。