-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMerge Statement Example.sql
61 lines (49 loc) · 1.93 KB
/
Merge Statement Example.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
SET NOCOUNT ON
GO
DROP TABLE IF EXISTS #SourceTable;
DROP TABLE IF EXISTS #TargetTable;
GO
-- Source Table
CREATE TABLE #SourceTable (
ID INT NOT NULL,
TableType VARCHAR(10) NOT NULL,
EmployeeName NVARCHAR(50) NOT NULL,
Department VARCHAR(10) NOT NULL,
ModifiedDate DATETIME DEFAULT GETDATE()
);
GO
-- Target Table
CREATE TABLE #TargetTable (
ID INT NOT NULL,
TableType VARCHAR(10) NULL,
EmployeeName VARCHAR(50) NOT NULL,
Department VARCHAR(10) NOT NULL,
IsMatched SMALLINT DEFAULT 0,
IsNotMatchedByTarget SMALLINT DEFAULT 0,
IsNotMatchedBySource SMALLINT DEFAULT 0,
ModifiedDate DATETIME DEFAULT GETDATE()
);
GO
-- INSERT statements for TargetTable
INSERT INTO #TargetTable (ID, TableType, EmployeeName, Department) VALUES (1, 'Target', 'Tommy Target', 'Accounting');
INSERT INTO #TargetTable (ID, TableType, EmployeeName, Department) VALUES (2, 'Target', 'Toni Target', 'Marketing');
INSERT INTO #TargetTable (ID, TableType, EmployeeName, Department) VALUES (4, 'Target', 'Trisha Target', 'IT');
-- INSERT statements for SourceTable
INSERT INTO #SourceTable (ID, TableType, EmployeeName, Department) VALUES (1, 'Source', 'Sally Source', 'Accounting');
INSERT INTO #SourceTable (ID, TableType, EmployeeName, Department) VALUES (2, 'Source', 'Sheila Source', 'Marketing');
INSERT INTO #SourceTable (ID, TableType, EmployeeName, Department) VALUES (3, 'Source', 'Sammy Source', 'Finance');
GO
--------------------------------------------------------------------
MERGE #TargetTable AS trgt
USING #SourceTable AS src
ON trgt.ID = src.ID AND trgt.ID > 0 AND src.ID > 0
WHEN MATCHED THEN
UPDATE SET trgt.IsMatched = 1
WHEN NOT MATCHED BY TARGET AND src.ID > 0 THEN
INSERT (ID, EmployeeName, Department, IsNotMatchedByTarget)
VALUES (src.ID, src.EmployeeName, src.Department, 1)
WHEN NOT MATCHED BY SOURCE AND trgt.ID > 0 THEN
UPDATE SET trgt.IsNotMatchedBySource = 1;
GO
SELECT * FROM #TargetTable;
GO