0

在处理包含行对象的 oracle 表时,我希望每一行都是一个对象,我可以在其上调用函数或将其传递给任何上下文中的函数。

例如,如果我声明以下内容:

create type scd_type as object
(
  valid_from date,
  valid_to date,
  member function get_new_valid_to return date
);

create type scd_type_table as table of scd_type;

create table scd_table of scd_type;

create procedure scd_proc (in_table in scd_type_table)
as
begin
  ... do stuff ...
end;
/

现在我尝试用表调用我的 proc

begin
  scd_proc (scd_table);
end;
/

我得到一个错误。即使将行读入嵌套表也不是直截了当的。我希望它像这样工作:

declare
  temp_table scd_type_table;
begin
  select * bulk collect into temp_table from scd_table;

  ... do stuff ...
end;
/

但相反,我必须为每一行调用构造函数。

最后,即使它在更新语句中工作,我也无法在合并语句中调用函数。例子:

update scd_table st
set st.valid_to = st.get_new_valid_to(); <--- Works.

merge into scd_table st
using (select sysdate as dateCol from dual) M
on (st.valid_from = M.dateCol)
when matched then update set st.valid_to = st.get_new_valid_to(); <--- Does not work.

所以我想这里有三个子问题:

1) 将行对象表传递到需要相同类型的嵌套表的过程中的最简单方法是什么?

2)将行对象表转换为相同类型的嵌套表的最简单方法是什么?

3) 为什么我不能在合并语句中调用对象上的函数(但在更新语句中)?

这一切都归结为“如何从行对象表中提取对象?”的问题。

4

1 回答 1

1

我不禁认为您需要重新阅读有关 PL/SQL types 的文档

您与批量收集代码很接近。小改动如下:

declare
  plsql_table scd_type_table;
begin
  select VALUE(t) bulk collect into plsql_table from scd_table t;
  -- do stuff
end;
/

我承认,我不知道为什么会merge失败,但会成功update

于 2012-07-20T05:03:42.320 回答