-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge.sql
More file actions
53 lines (43 loc) · 1.22 KB
/
merge.sql
File metadata and controls
53 lines (43 loc) · 1.22 KB
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
USE --insert database name here
GO
-- This merges two team member tables together. First check your target table to see which columns you need to update, or rows to insert...
SELECT *
FROM team.[TeamMember] AS TM
--now create your source table, which you want to merge with your target table.
CREATE TABLE dbo.testmerge
(
TeamId INT NOT NULL ,
UserID INT NOT NULL ,
AddedOn DATETIME NOT NULL ,
AddedBy INT NOT NULL
)
INSERT INTO [dbo].[testmerge]
( [TeamId], [UserID], [AddedOn], [AddedBy] )
VALUES ( 268, 1, GETDATE(), 1 ),
( 269, 1, GETDATE(), 1 ),
( 190, 1, GETDATE(), 1 ),
( 231, 1, GETDATE(), 1 )
SELECT *
FROM dbo.[testmerge] AS T
-- now create your merge
BEGIN TRANSACTION
MERGE team.[TeamMember] AS [TARGET]
USING [dbo].[testmerge] AS [SOURCE]
ON [Target].userid = [Source].userid
AND [target].teamid = [source].teamid
WHEN NOT MATCHED THEN
INSERT ( Teamid ,
UserID ,
AddedOn ,
Addedby
)
VALUES ( [Source].TeamID ,
[Source].UserID ,
[Source].Addedon ,
[Source].Addedby
)
OUTPUT
$ACTION ,
deleted.* ,
inserted.*;
ROLLBACK TRANSACTION