Search
 
SCRIPT & CODE EXAMPLE
 

SQL

sql merge

-- Oracle: Example for Insert or update in t1 from t2 values 
MERGE INTO table1 t1
USING table2 t2
ON (t1.CODE = t2.ID)
WHEN MATCHED THEN
    UPDATE SET t1.COL1 = t2.VALUE1
WHEN NOT MATCHED THEN
    INSERT (CODE, COL1)  VALUES (t2.ID, t2.VALUE1);
Comment

SQL Merge

MERGE target_table USING source_table
ON merge_condition
WHEN MATCHED
    THEN update_statement
WHEN NOT MATCHED
    THEN insert_statement
WHEN NOT MATCHED BY SOURCE
    THEN DELETE;
Code language: SQL (Structured Query Language) (sql)
Comment

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 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 :: install mysql on bash 
Sql :: date to string mariadb 
Sql :: dateadd in sql 
Sql :: mysql database is not starting in xampp 
Sql :: how to change column name in sql 
Sql :: identify number of rows in sql 
Sql :: mysql row_number() example 
Sql :: mysql show table fields 
Sql :: drop index in sql 
Sql :: concat column data in sql laravel 
Sql :: sql server locks 
Sql :: rename table column postgresql 
Sql :: sql create schema 
Sql :: postgres get defined index in table 
Sql :: oracle timestamp to date 
Sql :: sql select last id 
Sql :: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client vs code 
Sql :: oracle pagination query rownum 
Sql :: describe in sqlite3 
Sql :: write pandas dataframe to postgresql table psycopg2 
Sql :: sql insert multiple rows 
Sql :: check lock on table in sql server 
Sql :: sql server alter table add column tinyint 
Sql :: rename a table in sql server 
Sql :: DATEDIFF minute postgres 
Sql :: mysql récupérer le code création de vue 
Sql :: change mariadb to mysql xampp 
Sql :: sql if clause within where clause 
Sql :: MySQL INSERT IGNORE Statement 
Sql :: select distinct after join 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =