【问题标题】:oracle update collectionoracle 更新集合
【发布时间】:2014-05-23 17:01:58
【问题描述】:

我有两种用户定义的集合类型。第一种类型有列:

CREATE OR REPLACE
TYPE collection_1 as OBJECT
(
currency_code   varchar2(30),
amount   number
)

我已经使用 oracle bulk collect 在一个存储过程中填充了这个集合。所以现在让我们说,这个集合的值是这样的:

currency_code     amount
CAD               100
USD               50
CAD                120
USD               30

现在我想对这个集合执行一些聚合函数并填充另一个集合来存储每种货币的总金额。所以我定义了另一个这样的集合:

CREATE OR REPLACE
TYPE collection_2 as OBJECT
(
currency_code   varchar2(30),
total_amount   number
)

并像这样初始化它:

currency_code    total_amount
CAD               0
USD               0
GBP               0

现在我想遍历 collection_1 并填充 collection_2 以便 collection_2 如下所示:

currency_code    total_amount
CAD               220              --i.e.100+120
USD               80
GBP               0

我该怎么做?

【问题讨论】:

  • 如果您想聚合collection_1,那么为什么在填充collection_2 时对其进行迭代?你不能只用聚合的 collection_1 填充 collection_2 吗?

标签: plsql oracle11g


【解决方案1】:

可能是这样的。

declare
    TYPE t_coll_1 is TABLE OF collection_1;
    v_coll_1    t_coll_1;

    TYPE t_coll_2 is TABLE OF collection_2;
    v_coll_2          t_coll_2;

begin
    /* populate v_coll_1 */
    /* populate v_coll_2 */

    for i in v_coll_2.first.. v_coll_2.last
    loop
       for j in v_coll_1.first.. v_coll_1.last
       loop
          if v_coll_2(i).currency_code = v_coll_1(j).currency_code
          then
              v_coll_2(i).total_amount := v_coll_2(i).total_amount + v_coll_1(j).total_amount;
          end if;
       end loop;
    end loop;
end;

【讨论】:

  • 谢谢。我得出了确切的解决方案。感谢您的代码示例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多