【问题标题】:Update multiple rows with different values in Oracle在 Oracle 中用不同的值更新多行
【发布时间】:2016-09-02 19:29:33
【问题描述】:

我正在尝试使用 oracle 中的内部视图更新多行。

更新这个视图的select语句是:

select count(distinct a.numCount) as numCount, a.accNum as accNum ,
s.unitNum as unitNum 
from tableA a,tableS s  where a.accNum is not null and s.fk_id=
(select id from tableD where sid=a.accNum ) 
group by a.accNum ,s.unitNum ;

我正在尝试的更新声明如下:

update 
(select count(distinct a.numCount) as numCount, a.accNum as accNum ,
s.unitNum as unitNum 
from tableA a,tableS s  where a.accNum is not null and s.fk_id=
(select id from tableD where sid=a.accNum ) 
group by a.accNum ,s.unitNum ) k
set k.unitNum=k.numCount;

我正在尝试使用 numCount 的值更新 unitNum。 上述查询在用作视图时不起作用。 是否有另一种方法可以在 Oracle 中更新它。

请提出建议。

表格结构如下:

TableA

accNum   numCount
-----------------------
111        1
222        5
333        2
111        1
111        1
222        5
222        2


TableS

fk_id  unitNum 
-----------------------
123        0
768        0
734        0

TableD

ID      sid
-----------------------
123      222
768      111
734      333

输出应该如下:

TableS

fk_id  unitNum 
-----------------------
123        3
768        3
734        1

请推荐

【问题讨论】:

  • 上述查询是如何更新的?
  • 您能详细介绍一下表格结构吗?
  • 向我们展示update 声明
  • 更新了我的问题中的更新语句..但这不起作用..
  • 那么您得到的确切错误信息是什么?

标签: sql oracle sql-update


【解决方案1】:
update tableS s
   set unitNum=
        (select count(distinct a.numCount) as numCount
           from tableA a, tableD d
          where s.fk_id=d.id and d.sid=a.accNum
        );

【讨论】:

    猜你喜欢
    • 2011-04-22
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 2016-11-17
    • 1970-01-01
    相关资源
    最近更新 更多