2012-12-17 33 views
0

我有一個方案,我需要用戶合併SQL語句來同步兩個表。假設我有兩個表格A和表格B.與表格A中的一個額外列的例外情況相同。額外列是一個標誌,告訴我哪些記錄已準備好在表B中插入/更新。可以說該標誌列是IsReady。它將是真的或假的。合併SQL與Conditon

我可以在合併語句中使用Isready = True,或者我需要一個臨時表將所有記錄從表A移動到臨時表,其中IsReady = True,然後在TempTable和表B上使用合併SQL?

回答

1

是的,您可以在合併條件中使用該列。

merge tableB targetTable 
using tableA sourceTable 
on sourceTable.IsReady = 1 and [any other condition] 
when not matched then 
insert ... 
when matched and [...] then 
update ... 
+0

,如果我這樣做合併tableB的targetTable 上sourceTable.IsReady = 1和b.key =使用TABLEA sourceTable會 a.key它那個不工作合併兩個表,但不管a.Isready = True – InTheWorldOfCodingApplications

+0

在OP中發佈合併語句 - 您輸入了錯誤的內容。沒有理由不應該工作。 –

0

這可能會幫助你,

merge into tableB 
using tableA 
on tableB.IsReady=true 
when not matched then 
insert (tableB.field1,tableB.field2..) 
values (tableA.field1,tableA.field2..); 
commit;