简体   繁体   English

在oracle中更新多个嵌套表中的多个记录

[英]update multiple records in multiple nested tables in oracle

I have an oracle table with nested tables in some of the columns. 我有一个oracle表,在一些列中有嵌套表。 Now, I need to be able to update all the records in each nested table, in each of the records of the main table. 现在,我需要能够在主表的每个记录中更新每个嵌套表中的所有记录。 How is this accomplished? 这是如何完成的? Any of the ways that I've tried, I get errors about either not be able to perform updates on that view, or single row subquery returns more than one row. 我尝试过的任何方法都会导致错误,无法对该视图执行更新,或者单行子查询返回多行。

here's an example from to illustrate. 这里有一个例子来说明。 I can run an update like this: 我可以运行这样的更新:

    UPDATE TABLE(select entity.name
                 from entity
                 where entity.uidn = 2)
    SET last = 'Decepticon',
    change_date = SYSDATE,
    change_user = USER
    WHERE first = 'Galvatron';

but in this case, the table clause is being executed on a single nested table from a single row. 但在这种情况下,table子句正在从单行的单个嵌套表上执行。 How would an update like this be performed if you didn't want just the entity.uidn which equalled 2? 如果你不想只有等于2的entity.uidn,那么如何执行这样的更新?

thanks! 谢谢!

Perhaps the best reason for avoiding nested tables in a database is that they are hard to work with, and the syntax is underdocumented and hard to grok. 也许避免在数据库中使用嵌套表的最佳原因是它们难以使用,并且语法记录不足且难以理解。

Moving on! 继续!

Here is a table with a nested table. 这是一个带有嵌套表的表。

SQL> select f.force_name, t.id, t.name
  2  from transformer_forces f, table(f.force_members) t
  3  /

FORCE_NAME         ID NAME
---------- ---------- --------------------
Autobot             0 Metroplex
Autobot             0 Optimus Prime
Autobot             0 Rodimus
Decepticon          0 Galvatron
Decepticon          0 Megatron
Decepticon          0 Starscream
Dinobot             0 Grimlock
Dinobot             0 Swoop
Dinobot             0 Snarl

9 rows selected.

SQL>

As you can see, each element in the nested table the ID attribute is set to zero in all cases. 如您所见,嵌套表中的每个元素ID属性在所有情况下都设置为零。 What we would like to do is update all of them. 我们想要做的是更新所有这些。 But, alas! 可惜!

SQL> update table
  2   ( select force_members from transformer_forces ) t
  3  set t.id = rownum
  4  /
 ( select force_members from transformer_forces ) t
   *
ERROR at line 2:
ORA-01427: single-row subquery returns more than one row


SQL> 

It is possible to update all the elements on a nested table for a single row in the holding table: 可以更新保留表中单行的嵌套表上的所有元素:

SQL> update table
  2       ( select force_members from transformer_forces
  3         where force_name = 'Autobot') t
  4      set t.id = rownum
  5  /

3 rows updated.

SQL>

But the only way of doing that for the whole table is a PL/SQL loop. 但是对整个表执行此操作的唯一方法是PL / SQL循环。 Yuck! 呸!

There is an alternative: use a Nested Table Locator , via the NESTED_TABLE_GET_REFS hint. 还有一种方法:通过NESTED_TABLE_GET_REFS提示使用嵌套表定位器 This is a particularly obscure thing (it's not in the main list of hints ) but it does the trick: 这是一个特别模糊的事情(它不在主要的提示列表中 ),但它可以解决这个问题:

SQL> update /*+ NESTED_TABLE_GET_REFS */ force_members_nt
  2  set id = rownum
  3  /

9 rows updated.

SQL> select f.force_name, t.id, t.name
  2  from transformer_forces f, table(f.force_members) t
  3  /

FORCE_NAME         ID NAME
---------- ---------- --------------------
Autobot             1 Metroplex
Autobot             2 Optimus Prime
Autobot             3 Rodimus
Decepticon          4 Galvatron
Decepticon          5 Megatron
Decepticon          6 Starscream
Dinobot             7 Grimlock
Dinobot             8 Swoop
Dinobot             9 Snarl

9 rows selected.

SQL>

This hint allows us to bypass the holding table altogether and work with the actual nested table. 这个提示允许我们完全绕过保持表并使用实际的嵌套表。 That is, the object specified in the Nested Table storage clause: 也就是说,嵌套表存储子句中指定的对象:

create table transformer_forces (
    force_name varchar2(10)
    , force_members transformers_nt)
nested table force_members store as force_members_nt return as value;
                                    ^^^^^^^^^^^^^^^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM