Use of DB2 merge into statements

Source: Internet
Author: User


The use of the DB2 merge into statement the Merge statement in DB2 can MERGE the data in one table INTO another, and insert, delete, update, and other operations according to the conditions at the same time, very powerful. Syntax: Explain merge into table_name alias1USING (table | view | sub_query) alias2ON (join condition) when matched then update table_name SET col1 = col_val1, col2 = partition not matched then insert (column_list) the VALUES (column_values) keyword and the into clause specify the target table using clause to be modified or inserted in the into clause to specify the data source to be modified or inserted in the using clause. The data source can be a table, view, or subquery statement. The on clause specifies in the on clause that the insert or modify operation meets the conditions. When matched | not matched uses this clause to notify the database of operations on results that meet or do not meet the conditions. You can use the following two types of clauses.
The merge_update clause modifies the Field Values in the target table. Executed when the on Clause condition is met. If the modification clause is executed, the modification trigger on the target table is triggered.
Restriction: when modifying a view, you cannot specify a default value for the merge_insert clause to insert data to the target table when the on Clause condition is not met. If the insert clause is executed, the insert trigger on the target table is triggered. Restriction: when modifying a view, you cannot specify a default value example: merge into bonuses d using (SELECT EMPLOYEE_ID, SALARY, DEPARTMENT_ID from employees where DEPARTMENT_ID = 80) s on (D. EMPLOYEE_ID = S. EMPLOYEE_ID) when matched then update set d. BONUS = D. BONUS + S. SALARY *. 01 when not matched then insert (D. EMPLOYEE_ID, D. BONUS) VALUES (S. EMPLOYEE_ID, S. SALARY * 0.01); merge into xforwartransferstate as t using table (VALUES (?,?,?,?,?,?)) T1 (DATASNO, EXTERIORSYSTEM, STATUS, DATA_TIME, MARKFORDELETE, DATATRANSFERNO) ON (T. DATATRANSFERNO = T1.DATATRANSFERNO) when matched then update set T. DATASNO = T1.DATASNO, T. EXTERIORSYSTEM = T1.EXTERIORSYSTEM, T. STATUS = T1.STATUS, T. LASTUP_TIME = T1.DATA _ TIME, T. MARKFORDELETE = T1.MARKFORDELETE when not matched then insert (DATASNO, EXTERIORSYSTEM, STATUS, CREATE_TIME, MARKFORDELETE, DATATRANSFERNO) VALUES (T1.DATASNO, summary, T1.STATUS, T1.DATA _ TIME, T1.MARKFORDELETE, t1.DATATRANSFERNO merge into employe as em using manager as ma on em. EMPLOYEID = MA. managerid when matched and em. SALARY <MA. salary then update set em. SALARY = MA. salary when matched and em. SALARY> MA. salary then signal sqlstate '200' SET MESSAGE_TEXT = 'em. SALARY> MA. SALARY 'when not matched then insert values (MA. MANAGERID, MA. NAME, MA. SALARY) else ignore;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.