Oracle“pl/sql”集合是按值分配还是按引用分配?

Ant*_*ton 2 oracle plsql

我有一个集合,需要将此集合复制到另一个集合。

例如:

TYPE my_table_type IS TABLE OF some_object;

procedure test(p_table IN my_table_type) is
  internal_table my_table_type;
begin
  internal_table := p_table;

  internal_table.extend;
  .... 
end;
Run Code Online (Sandbox Code Playgroud)

这是按引用赋值还是按值赋值?

这是复制嵌套表的正确方法吗?

我努力了。看起来效果很好。但我在 Oracle 文档中找不到任何参考。

Pav*_*nov 5

The assignment in your example internal_table := p_table; is assignment by value.

Here's a little test to show how it works:

set serveroutput on
declare 
   TYPE my_table_type IS TABLE OF integer;
   table_ my_table_type := new my_table_type();
   internal_table my_table_type;
begin
  table_.extend(1);
  table_(1) := 123;
  dbms_output.put_line(table_(1)); --output is 123
  internal_table := table_;
  dbms_output.put_line(internal_table(1)); --output is 123


  table_(1) := 321;
  dbms_output.put_line(table_(1)); --output is 321
  dbms_output.put_line(internal_table(1)); --output is 123
end;
Run Code Online (Sandbox Code Playgroud)

As you can see, the value in internal_table did not change which means we have two different collections.

Now, if you're talking about the input parameter of test procedure, it can be passed either by reference, or by value, which depends on argument's mode:

IN parameters are always passed by reference.
IN OUT or OUT parameters, by default, are passed by value. But, if you specify the NOCOPY option, they will be passed by reference.

You can read more about it here Subprogram Parameter Modes