SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Services
  • Product
    🏋️ GyManage 📱 More to come
Contact
  1. SQL
  2. SQL Merge

Text copied!

« Previous
Next »

SQL Merge

July 6, 2023, 10:45 a.m. under SQL

  • Hi! You should check SQL Views post first.

    MERGE is a powerful SQL statement used to perform insert, update, or delete operations on a target table based on the results of a join with a source table.

    Merge allows you to keep two tables in sync easily or perform upserts (update existing rows or insert new rows if they do not exist).

    Here's a general syntax for MERGE :
    MERGE INTO target_table AS target
    USING source_table AS source
    ON <join_condition>
    WHEN MATCHED THEN
        UPDATE SET <update_clause>
    WHEN NOT MATCHED THEN
        INSERT (<column_list>) VALUES (<value_list>)
    WHEN NOT MATCHED BY SOURCE THEN
        DELETE;
            

    • target_table : The table you want to change.

    • source_table : The table providing the data for the changes.

    • <join_condition> : How to match rows between the two tables.

    • <update_clause> : Which columns to update and what values to use, for matched rows.

    • <column_list> and <value_list> : Columns and their values to add to the target table if no match is found.

    • WHEN NOT MATCHED BY SOURCE THEN DELETE: This part is optional and It deletes rows from the target table that don't have matches in the source table.

    Here's a simplified example of using the MERGE statement in SQL :

    1. Let's assume you have a database with two tables : [i]. Product [ii]. Product2

    SQL Merge

    2. The [Product] table houses 3 records, while the [Product2] table currently accommodates only 1 record sourced from the Product table, where the product_name is NULL.

    3. Now, you want to update the [Product2] table with the data from [Product], performing inserts for new product and updates for existing ones. Run below SQL statement :

    MERGE INTO [Product2] AS target
    USING [Product] AS source
    ON (target.Product_Id = source.Product_Id)
    WHEN MATCHED THEN
        UPDATE SET target.Product_Id= source.Product_Id,
                   target.[Name]= source.[Name],
                   target.GroupName = source.GroupName,
                   target.ModifiedDate = source.ModifiedDate
    WHEN NOT MATCHED THEN
        INSERT (Product_Id, [Name], GroupName, ModifiedDate) 
        VALUES (source.Product_Id,source.[Name], source.GroupName, source.ModifiedDate);
            
    SQL Merge

    4. This MERGE statement will update existing records in [Product2] with matching Department_Id from [Product] and insert new records from Product into [Product2].

    SQL Merge

    Great job! You've successfully merged the tables!

    Frequently Asked Questions :

    What is MERGE in SQL?

    MERGE in SQL is a statement used to perform insert, update, or delete operations on a target table based on the results of a join with a source table.

    How to MERGE database in SQL?

    To MERGE databases in SQL, you typically use backup and restore operations or data migration techniques.

    How to MERGE two SQL queries?

    To MERGE two SQL queries, you can use UNION or UNION ALL operators to combine their results into a single result set.

    How to MERGE 2 tables in SQL?

    To MERGE two tables in SQL, you can use the MERGE statement along with appropriate join conditions and actions to synchronize or update data between them.
    Thank You! You should check SQL CTE post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    IIF function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL CTE

    SQL WITH TIES

    Define Transaction in DBMS

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India

    © 2024 Copyright | All Rights Reserved | Legal