【问题标题】:Oracle Temporary Storage Within a Function函数内的 Oracle 临时存储
【发布时间】:2013-11-07 21:40:34
【问题描述】:

假设我有一个函数,我需要在其中执行多个操作,所有这些操作都取决于一个查询的结果。我能找到的一切都表明我需要在过程之外定义一个临时表,我不想这样做。

我想做如下的事情:

create or replace function f_example(
  a_input in number
)
return varchar2 is
begin
  create local temporary table tempIDs
  ( 
    testID number(6, 0)
    , testValue number(8,0)
  );

  //select data from tableFoo that will be overwritten by a_input into tempIDs

  //update data in tableFoo with a_input & store old data in another tableFoo field

end f_example;

这种语法不起作用。 Oracle 不允许在函数中“创建”。

我并不是真正的数据库程序员——我习惯于使用 C# 和 Java 工作。在这种情况下,我会将我的值存储在方法完成时超出范围的本地数组(或其他)中。在 Oracle SQL 中是否合法地没有办法做这样的事情?

【问题讨论】:

  • PL/SQL 和 Java 一样有集合和数组。
  • 根据代码示例中的cmets,您根本不需要临时存储,只需执行update tableFoo set a_field = a_input, another_field = a_field where ...
  • 我面临的问题(我的示例没有足够清楚地传达)是我需要能够在更新后引用旧值。

标签: sql oracle function temp-tables


【解决方案1】:

您可以定义 PL/SQL 记录类型和关联的表类型。然后您可以发出SELECT...BULK COLLECT 来填满您的表格。

declare
  type my_record_type is record (testId number, testvalue number);
  type my_table_type is table of my_record_type index by binary_integer;
  my_table my_table_type;
begin
  select x, y bulk collect into my_table from table_foo;
end;

【讨论】:

  • 这似乎可行,但我该如何引用其中的数据?当我在填充后尝试从 my_table 中选择时,出现编译器错误:ORA-00942,表或视图不存在。
  • 您不会从 my_table 中“选择”。它实际上是一个对象数组,而不是一个表。 Oracle 的命名约定有些误导。你如何引用它取决于你想做什么,但你可以FOR 循环它,或者做一个FORALL 操作。
猜你喜欢
  • 2015-07-05
  • 2011-12-22
  • 1970-01-01
  • 2011-10-29
  • 2015-02-15
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
相关资源
最近更新 更多