Search
 
SCRIPT & CODE EXAMPLE
 

SQL

sql merge statement

MERGE LoginTypes T
        USING (SELECT 'System' as Description) S
        ON(S.Description = T.Description)
WHEN NOT MATCHED BY TARGET
    THEN INSERT(Description, CreatedTimestamp, LastUpdatedTimestamp)
VALUES('System', getdate(), getdate());
Comment

merge command in sql

USE SqlShackMergeDemo
GO
    
MERGE TargetProducts AS Target
USING SourceProducts	AS Source
ON Source.ProductID = Target.ProductID
    
-- For Inserts
WHEN NOT MATCHED BY Target THEN
    INSERT (ProductID,ProductName, Price) 
    VALUES (Source.ProductID,Source.ProductName, Source.Price)
    
-- For Updates
WHEN MATCHED THEN UPDATE SET
    Target.ProductName	= Source.ProductName,
    Target.Price		= Source.Price
    
-- For Deletes
WHEN NOT MATCHED BY Source THEN
    DELETE;
Comment

merge in sql

/* Selecting the Target and the Source */
MERGE PRODUCT_LIST AS TARGET
    USING UPDATE_LIST AS SOURCE
 
    /* 1. Performing the UPDATE operation */
 
    /* If the P_ID is same,
       check for change in P_NAME or P_PRICE */
    ON (TARGET.P_ID = SOURCE.P_ID)
    WHEN MATCHED
         AND TARGET.P_NAME <> SOURCE.P_NAME
         OR TARGET.P_PRICE <> SOURCE.P_PRICE
 
    /* Update the records in TARGET */
    THEN UPDATE
         SET TARGET.P_NAME = SOURCE.P_NAME,
         TARGET.P_PRICE = SOURCE.P_PRICE
      
    /* 2. Performing the INSERT operation */
 
    /* When no records are matched with TARGET table
       Then insert the records in the target table */
    WHEN NOT MATCHED BY TARGET
    THEN INSERT (P_ID, P_NAME, P_PRICE)         
         VALUES (SOURCE.P_ID, SOURCE.P_NAME, SOURCE.P_PRICE)
 
    /* 3. Performing the DELETE operation */
 
    /* When no records are matched with SOURCE table
       Then delete the records from the target table */
    WHEN NOT MATCHED BY SOURCE
    THEN DELETE
 
/* END OF MERGE */
Comment

PREVIOUS NEXT
Code Example
Sql :: postgresql create table add unique constraints 
Sql :: except in sql 
Sql :: mysql select smaller of two values 
Sql :: mysql create table 
Sql :: create procedure 
Sql :: install pymysql in python 3 in windows 7 v2.7.10 codes with pip 
Sql :: mariadb cast null to 0 
Sql :: insert ip address in mysql 
Sql :: long string type sql 
Sql :: sql select statements 
Sql :: sql: extract day text from datetime value 
Sql :: sql commands 
Sql :: sql cross apply vs join 
Sql :: snowflake insert select 
Sql :: postgresql connect 
Sql :: mysql filter based on datediff 
Sql :: incorrect datetime value sql table error 1292 
Sql :: synonym oracle 
Sql :: sql not 
Sql :: sql procedure 
Sql :: doctors appointment 
Sql :: install sql server management studio ubuntu 
Sql :: what is between operator 
Sql :: select limit ms sql 
Sql :: sql table contains 
Sql :: increase space oracle aws instance 
Sql :: knex last insert id mysql 
Sql :: SQL - Row Number into Alphabetical characters 
Sql :: SQL BETWEEN OPERATOR With Texts 
Sql :: python sqlalcahmey compare datetime 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =