0

プロシージャの作成中に設計上の問題が発生しました。

同じ行の他の列のデータを使用して、テーブルのすべての行を更新する必要があるとします。table13 つの列がAありBCすべての行を として更新する必要があるとしますC=A+B。だから私は使用することができます:

update table1 set C=A+B;

しかし、私は以下のようなものを使用してこれを行う必要があります:

merge tab1e1 using (some query) on (some condition)
when matched update
 C=A+B
when not matched 
null;

「クエリ」と「条件」を操作してこれを行う方法はありますか?

4

1 に答える 1

1

更新の代わりにマージを使用する理由がよくわかりませんが、本当に必要な場合は、句と常に真である条件dualを作成するために使用できます。usingon

merge into table1
using (select null from dual)
on (1 = 1)
when matched then update set c = a + b;

いくつかのサンプルデータを使用:

create table table1 (a number, b number, c number);
insert into table1 values (1, 2, null);
insert into table1 values (3, 4, null);
insert into table1 values (5, 6, null);

merge into table1
using (select null from dual)
on (1 = 1)
when matched then update set c = a + b;

3 rows merged.

select * from table1;

         A          B          C
---------- ---------- ----------
         1          2          3 
         3          4          7 
         5          6         11 

SQLフィドル

于 2014-07-01T18:36:24.297 に答える