使用基于SQL Server上相同表的视图更新包含计算值的表

时间:2022-03-12 23:08:03

I have a table

我有一张桌子

tblCurrent:

  Ref  |   . .... .  . . |  Total A   |   Total B
 A001  |                 |    NULL    |     NULL
 A002  |                 |    NULL    |     NULL
 A003  |                 |    NULL    |     NULL
 A004  |                 |    NULL    |     NULL

I've created a view vwCurrentB that provides a count of records that meet specific criteria in the same table.

我创建了一个视图vwCurrentB,它提供了满足同一表中特定条件的记录计数。

How can I updated fields Total A and Total B with relevant values from the view. Both the table and view will have exactly the same number of records, with Ref being a unique key. I just need to get the additional two columns in the view, into their respective records in the table?

如何使用视图中的相关值更新字段总计A和总计B.表和视图都具有完全相同的记录数,Ref是唯一的键。我只需要在视图中获取额外的两列,进入表中各自的记录?

1 个解决方案

#1


3  

update tc
set TotalA = v.TotalA
   ,TotalB = v.TotalB
from tblCurrent tc
join vwCurrentB v on tc.Ref = v.Ref  

#1


3  

update tc
set TotalA = v.TotalA
   ,TotalB = v.TotalB
from tblCurrent tc
join vwCurrentB v on tc.Ref = v.Ref