简体   繁体   English

Oracle更新加入

[英]Oracle Update Join

I would like to update a table by referencing values within it's own row and also it's parent record in the same table. 我想通过引用表自身行中的值以及同一表中的父记录来更新表。

I have a table called import, and it needs to update the child record's chain_id to it's parent's chain_id if its currently set to 0 and the parent isn't 0. Also set the child records status to 1 if the action is 3 otherwise leave it as it was, and also set the parent status to 0. 我有一个名为import的表,如果当前将其设置为0且父级不为0,则需要将子记录的chain_id更新为其父级的chain_id。如果操作为3,则还将子级记录的状态设置为1,否则将其保留保持原样,并将父状态设置为0。

Here is some non-working code that illustrates what I'm trying to do: 这是一些无法正常工作的代码,这些代码说明了我要执行的操作:

update 
(
select
c.chain_id as c_chain_id,
c.status as c_status,
c.action as c_action,
p.chain_id as p_chain_id,
p.status as p_status
from import c
join import p on c.original_dissemination_id = p.dissemination_id
where c.chain_id = 0 and
p.chain_id <> 0 and
)
set
c_chain_id = p_chain_id,
c_status = (if c_action = 3 then return 1 else c_status), /* 3 if action = 1 else leave it as it was */
p_status = 0

Can someone please translate the above to some oracle code that works? 有人可以将以上内容翻译为有效的oracle代码吗? thanks 谢谢

Do you tried to fix it with hierarchical queries? 您是否尝试通过层次查询来修复它?

See this examples: 请参阅以下示例:

   http://docs.oracle.com/cd/B19306_01/server.102/b14200/queries003.htm

Regards and good luck 问候和好运

The problem I see is you're wanting preform 2 updates at the same time. 我看到的问题是您要同时更新瓶坯2。 Effectively, what you have are two tables c and p with a hierarchical relation. 实际上,您拥有的是两个具有层次关系的表c和p。 I don't know of a sql only way in oracle to update 2 tables at once. 我不知道在oracle中一次只能更新2个表的SQL唯一方式。 There are some tricks you could do with views and possibly some really fancy SQL. 您可以使用一些技巧来处理视图,也可以使用一些非常漂亮的SQL。

However, fancy is not always the best solution. 但是,花式不一定总是最好的解决方案。

I would recommend using a little pl/sql such as 我建议使用一点pl / sql如

BEGIN
  FOR v_rec IN (SELECT c.chain_id AS c_chain_id,
                     c.status AS c_status,
                     c.action AS c_action,
                     p.chain_id AS p_chain_id,
                     p.status AS p_status,
                     c.dissemination_id AS c_dissemination_id,
                     p.dissemination_id AS p_dissemination_id
                FROM import c
                JOIN import p ON c.original_dissemination_id = p.dissemination_id
               WHERE c.chain_id = 0
                 AND p.chain_id <> 0) LOOP

    UPDATE import
       SET chain_id = v_rec.p_chain_id,
           status = decode(v_rec.c_action, 3, 1, status)
     WHERE dissemination_id = v_rec.c_dissemination_id;

    UPDATE import
       SET chain_id = 0
     WHERE dissemination_id = v_rec.p_dissemination_id;

    commit;

  END LOOP;

END;
/

This should loop through all the records as you defined in your query above (I added what I'm assuming are the pk and fk columns to the output.) This will do the 2 updates and the decode will handle the action/status part. 这将遍历您在上面的查询中定义的所有记录(我在输出中添加了pk和fk列)。这将进行2次更新,并且解码将处理操作/状态部分。

You'll of course, want to test this and put some exception handling in as needed. 当然,您需要对此进行测试,并根据需要进行一些异常处理。

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

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