1
SQL Server合併聲明
MERGE DestinationTable AS D
USING @SourceTable AS S
ON D.Alternate_ID = S._ID
WHEN MATCHED AND
(
D.Candidate_ID <> S.Candidate_ID OR ISNULL(D.Vacancy_ID,'') <> S.Vacancy_ID
)
THEN
UPDATE SET
D.FName = S.FName,
D.Department = S.Department,
WHEN NOT MATCHED BY TARGET
THEN INSERT
(
Alternate_ID,
FName,
Department
)
VALUES
(
S.ID,
S.FName,
S.Department
)
WHEN NOT MATCHED BY SOURCE
--How to add a where clause to the delete statement here
THEN DELETE; --E.g WHERE D.Department <> 'HR'
我在上面使用的MERGE語句刪除destinationTable會記錄如何添加一個地方,如果他們已經從源刪除條件爲刪除
是否有任何人知道如何可以將WHERE條件添加到Delete語句中?我只想從ColA等於特定常量字符串的目標中刪除。
根據http://sqlstudies.com/2013/09/30/the-merge-statement-doesnt-have-a-where-clause/,您可以添加到何時不匹配的來源和D .Department <>'HR'THEN DELETE – 2014-09-29 09:39:00