DB2 Insert or update solution (use Merge into to resolve a record, or a multiple-table relationship problem)

Source: Internet
Author: User
Tags db2 db2 insert

Merge into Table1 t1

Using (select Column1, column2 from table2) T2

On t1.column1 = T2.column1

When matched then update set t1.column2 = T2.column2

When not matched then insert values (COLUMN1,COLUMN2)

The above is to insert or update the data in the table2 into the Table1.

But for developers, we're working with a single record to determine whether this data is an insert or an update.

The treatment for DB2 is as follows:

Merge into Table1 t1

using (
Data in the Select Program 1 as Column1,
The data in the program is 2 as Column2 from SYSIBM. SYSDUMMY1
) T2

To ensure that T2 must have a data, is the data in your program, SYSIBM. SYSDUMMY1 is DB2 specific, and of course there are other ways you can handle it, such as SELECT COUNT (1) from any table.


On a.column1= b.column1

When matched then update set t1.column2 = T2.column2

When not matched then insert values (COLUMN1,COLUMN2)

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.