比较不同数据库中的两个 table 并更改一个 table

Compare two tables in different databases and change one table

我在同一个 sql 实例中有两个数据库。一个是 backup2,它是我原始数据库的还原备份。

Database Table
Original.Payments
Backup2.Payments

我有两个字段需要比较:

PaymentsId - guid
IsProcessed - bit

我需要比较每个中的 PaymentsId,如果付款存在于 Backup2 中并标记为已处理,我需要将 Original.Payments.Backup 标记为 true。

我已经完成了查询的第一部分,但我不确定如何 link 将其 link 到原始数据库:

SELECT [PaymentId]
      ,[CorporationId]
      ,[IsProcessed]
  FROM [Backup2].[Web].[Payment]
 WHERE CorporationId = '2aa2dfw-20d2-4694-8e01-72288a1e8d4' 
   and IsProcessed = 'true' 

这提供了我的付款清单,但我需要将它们与原始数据库进行比较,但我不确定如何进行比较。

我从这里去哪里?

谢谢!

这应该是一个开始

SELECT [p1].[PaymentId]
      ,[p1].[CorporationId]
      ,[p1].[IsProcessed]
  FROM [Backup2].[Web].[Payment]  as [p1]
  JOIN [Original].[Web].[Payment] as [p2]
    on [p2].[CorporationId] = [p1].[CorporationId] 
   AND [p2].[PaymentId] = [p1].[PaymentId] 
   AND [p1].[CorporationId] = '2aa2dfw-20d2-4694-8e01-72288a1e8d4' 
   and [p1].[IsProcessed =]'true' 

您可以使用 updatejoin 语法

update OP
set IsProcessed = 'true'
FROM [Original].[Payments].[Backup] OP
JOIN [Backup2].[Web].[Payment] BP
on OP.PaymentId = BP.PaymentId
and BP.corporationId = '2aa2dfw-20d2-4694-8e01-72288a1e8d4'
and BP.IsProcessed ='true'
and OP.corporationId = '2aa2dfw-20d2-4694-8e01-72288a1e8d4'