我在同一个sql实例中有两个数据库。一个是backup2,它是我原始数据库的恢复备份。
Database Table
Original.Payments
Backup2.Payments
我需要比较两个字段:
PaymentsId - guid
IsProcessed - bit
我需要比较每个中的PaymentsId,如果Backup2中存在付款且标记为已处理,我需要将Original.Payments.Backup标记为true。
我已完成查询的第一部分,但我不确定如何将其链接到原始数据库:
SELECT [PaymentId]
,[CorporationId]
,[IsProcessed]
FROM [Backup2].[Web].[Payment]
WHERE CorporationId = '2aa2dfw-20d2-4694-8e01-72288a1e8d4'
and IsProcessed = 'true'
这给了我我的付款清单,但我需要将它们与原始数据库进行比较,我不确定如何。
我从哪里开始?
谢谢!
答案 0 :(得分:1)
您可以update
使用join
语法
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'
答案 1 :(得分:0)
这应该是一个开始
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'