{"id":1935,"date":"2013-06-06T03:47:08","date_gmt":"2013-06-06T03:47:08","guid":{"rendered":"http:\/\/www.withdata.com\/ad\/?p=1935"},"modified":"2024-08-17T05:29:53","modified_gmt":"2024-08-17T05:29:53","slug":"replace-update-or-insert-a-row-into-db2-table-merge-into","status":"publish","type":"post","link":"https:\/\/www.withdata.com\/blog\/db2\/replace-update-or-insert-a-row-into-db2-table-merge-into.html","title":{"rendered":"Replace (Update\/Insert) a row into DB2 table &#8211; Merge into"},"content":{"rendered":"<p>In MySQL, \u00a0if you want to either updates or inserts a row in a table, depending if the table already has a row that matches the data, you\u00a0can use &#8220;ON DUPLICATE KEY UPDATE&#8221;.<\/p>\n<p>How to do it in DB2?<\/p>\n<p>Use &#8220;merge into&#8221; .<\/p>\n<pre>MERGE INTO table_to_upsert AS tab\r\nUSING (VALUES\r\n        (1, 2, 3),\r\n        (4, 5, 6),\r\n        (7, 8, 9)\r\n        -- more rows\r\n    ) AS merge (C1, C2, C3)\r\n    ON tab.key_to_match = merge.key_to_match\r\n    WHEN MATCHED THEN\r\n        UPDATE SET tab.C1 = merge.C1,\r\n                   tab.C2 = merge.C2,\r\n                   tab.C3 = merge.C3\r\n    WHEN NOT MATCHED THEN\r\n        INSERT (C1, C2, C3)\r\n        VALUES (merge.C1, merge.C2, merge.C3)<\/pre>\n<p>For example:<\/p>\n<pre>MERGE INTO employees AS tab\r\nUSING (VALUES\r\n        (123456,'smith','bob')\r\n    ) AS merge (id,last_name,first_name)\r\n    ON tab.id = merge.id\r\n    WHEN MATCHED THEN\r\n        UPDATE SET tab.id = merge.id,\r\n                   tab.last_name = merge.last_name,\r\n                   tab.first_name = merge.first_name\r\n    WHEN NOT MATCHED THEN\r\n        INSERT (id,last_name,first_name)\r\n        VALUES (merge.id, merge.last_name, merge.first_name)<\/pre>\n<p>&nbsp;<\/p>\n<p>See also:<\/p>\n<p><a href=\"http:\/\/www.withdata.com\/blog\/txttodb2\/upsert-data-from-csv-file-to-db2-without-programming.html\"> Upsert data from CSV to DB2<\/a><br \/>\n<a href=\"http:\/\/www.withdata.com\/blog\/txttodb2\/upsert-data-from-tsv-file-to-db2-without-programming.html\"> Upsert data from TSV to DB2<\/a><br \/>\n<a href=\"http:\/\/www.withdata.com\/blog\/txttodb2\/upsert-data-from-txt-file-to-db2-without-programming.html\"> Upsert data from TXT to DB2<\/a><br \/>\n<a href=\"http:\/\/www.withdata.com\/blog\/xlstodb2\/upsert-data-from-excel-file-to-db2-without-programming.html\"> Upsert data from Excel to DB2<\/a><br \/>\n<a href=\"http:\/\/www.withdata.com\/blog\/xmltodb2\/upsert-data-from-xml-file-to-db2-without-programming.html\"> Upsert data from XML to DB2<\/a><br \/>\n<a href=\"http:\/\/www.withdata.com\/blog\/jsontodb2\/upsert-data-from-json-file-to-db2-without-programming.html\"> Upsert data from JSON to DB2<\/a><br \/>\n<a href=\"http:\/\/www.withdata.com\/blog\/sqltodb2\/upsert-data-from-sql-file-to-db2-without-programming.html\"> Upsert data from SQL file to DB2<\/a><\/p>\n<p>Replace (Update\/Insert) a row into other DB:<\/p>\n<p>In SQL Server,\u00a0<a href=\"https:\/\/www.withdata.com\/blog\/sql-server\/replace-update-or-insert-a-row-into-sql-server-table.html\">https:\/\/www.withdata.com\/blog\/sql-server\/replace-update-or-insert-a-row-into-sql-server-table.html<\/a>\u00a0.<\/p>\n<p>In Oracle,\u00a0<a href=\"https:\/\/www.withdata.com\/blog\/oracle\/replace-update-or-insert-a-row-into-oracle-table-merge-into.html\">https:\/\/www.withdata.com\/blog\/oracle\/replace-update-or-insert-a-row-into-oracle-table-merge-into.html<\/a><\/p>\n<p>In PostgreSQL,\u00a0<a href=\"https:\/\/www.withdata.com\/blog\/postgresql\/replace-update-or-insert-a-row-into-postgresql-table.html\">https:\/\/www.withdata.com\/blog\/postgresql\/replace-update-or-insert-a-row-into-postgresql-table.html<\/a><\/p>\n<p>In Sqlite,\u00a0<a href=\"https:\/\/www.withdata.com\/blog\/sqlite\/replace-update-or-insert-a-row-into-sqlite-table.html\">https:\/\/www.withdata.com\/blog\/sqlite\/replace-update-or-insert-a-row-into-sqlite-table.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In MySQL, \u00a0if you want to either updates or inserts a row in a table, depending if the table already has a row that matches the data, you\u00a0can use &#8220;ON DUPLICATE KEY UPDATE&#8221;. How to do it in DB2? Use &#8220;merge into&#8221; . MERGE INTO table_to_upsert AS tab USING (VALUES (1, 2, 3), (4, 5, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1318,238,239,428],"class_list":["post-1935","post","type-post","status-publish","format-standard","hentry","category-db2","tag-db2","tag-merge-into","tag-replace-into","tag-upsert"],"_links":{"self":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/1935","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/comments?post=1935"}],"version-history":[{"count":9,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/1935\/revisions"}],"predecessor-version":[{"id":21730,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/1935\/revisions\/21730"}],"wp:attachment":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/media?parent=1935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/categories?post=1935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/tags?post=1935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}