sql merge statement

Solutions on MaxInterview for sql merge statement by the best coders in the world

showing results for - "sql merge statement"
Julieta
05 May 2019
1MERGE esqlProductTarget T
2USING esqlProductSource S
3ON (S.ProductID = T.ProductID)
4WHEN MATCHED 
5     THEN UPDATE
6     SET    T.Name = S.Name,
7            T.ProductNumber = S.ProductNumber,
8            T.Color = S.Color
9WHEN NOT MATCHED BY TARGET
10THEN INSERT (ProductID, Name, ProductNumber, Color)
11     VALUES (S.ProductID, S.Name, S.ProductNumber, S.Color)
12WHEN NOT MATCHED BY SOURCE
13THEN DELETE;
Bruno
17 Feb 2018
1MERGE dbo.MyTarget targ
2USING (SELECT ... FROM dbo.MySource GROUP BY .....) src
3ON (targ.Identifier = src.Identifier
4    AND targ.Name = src.ConstituentName
5    AND targ.Ticker = src.ConstituentTicker
6    AND (targ.CUSIP = src.CUSIP OR targ.ISIN = src.ISIN OR targ.SEDOL = src.SEDOL))
7WHEN MATCHED THEN
8-- update values
9;
Bobbi
09 Aug 2020
1MERGE LoginTypes T
2        USING (SELECT 'System' as Description) S
3        ON(S.Description = T.Description)
4WHEN NOT MATCHED BY TARGET
5    THEN INSERT(Description, CreatedTimestamp, LastUpdatedTimestamp)
6VALUES('System', getdate(), getdate());