我有一个客户信息表 - [Customer Alignments]
即主表。
随着事情的发展,还有另一个表格可以帮助客户编制客户信息。这称为[tCC Master]
所以,我想要做的就是使用此[tCC Master]
表并检查[Customer Number]
和[Therapy]
字段中的信息是否与[Customer Alignments]
不同。如果有,我想更新这些字段,以便从[Customer Number]
[Therapy]
和/或[tCC Master]
即:
[Customer Alignments]
- Number - Therapy - Location Number
- 1 - ABC - 1234
- 2 - BCD - 5678
- 3 - YES - 9101
- 4 - ABC - 0246
[tCC Master]
- Number - Therapy - Location
- 1 - BCD - 1234
- 5 - QWE - 5678
- 3 - YES - 9101
- 6 - ABC - 0246
这就是我所拥有的,但我不确定
UPDATE [dbo].[ru_t_Customer Alignments]
SET [dbo].[ru_t_Customer Alignments].[Customer Number] = [dbo].[ru_tCC_Master].[Customer Number] (@[dbo].[ru_t_Customer Alignments].[Customer Number], [dbo].[ru_t_Customer Alignments].[Customer Number])
, [dbo].[ru_t_Customer Alignments].[Therapy] = [dbo].[ru_tCC_Master].[Therapy] (@[dbo].[ru_t_Customer Alignments].[Therapy], [dbo].[ru_t_Customer Alignments].[Therapy])
FROM [dbo].[ru_t_Customer Alignments], [dbo].[ru_tCC_Master]
WHERE [dbo].[ru_t_Customer Alignments].[Customer Number] <> [dbo].[ru_tCC_Master].[Customer Number] OR [dbo].[ru_t_Customer Alignments].[Therapy] = [dbo].[ru_tCC_Master].[Therapy];
如果有人可以帮我指点解决方案,那就太棒了。 谢谢!
答案 0 :(得分:0)
首先你应该绑定两个表。如果数据不同,则更新该列。 我假设这些表中的位置信息较新,并且它们对所有客户都是唯一的。
我还给表提供了短的别名,以便于理解。
UPDATE CA
SET CA.[Customer Number] = CM.[Customer Number],
CA.[Therapy] = CM.[Therapy]
FROM [dbo].[ru_t_Customer Alignments] CA
INNER JOIN [dbo].[ru_tCC_Master] CM ON CA.[Location Number] = CM.[Location]
WHERE CA.[Customer Number] <> CM.[Customer Number]
OR CA.[Therapy] <> CM.[Therapy] ;