<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>OpenText Analytics Database 26.2.x – Merging table data</title>
    <link>/en/admin/working-with-native-tables/merging-table-data/</link>
    <description>Recent content in Merging table data on OpenText Analytics Database 26.2.x</description>
    <generator>Hugo -- gohugo.io</generator>
    
	  <atom:link href="/en/admin/working-with-native-tables/merging-table-data/index.xml" rel="self" type="application/rss+xml" />
    
    
      
        
      
    
    
    <item>
      <title>Admin: Basic MERGE example</title>
      <link>/en/admin/working-with-native-tables/merging-table-data/basic-merge-example/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/admin/working-with-native-tables/merging-table-data/basic-merge-example/</guid>
      <description>
        
        
        &lt;p&gt;In this example, a merge operation involves two tables:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;visits_daily&lt;/code&gt; logs daily restaurant traffic, and is updated with each customer visit. Data in this table is refreshed every 24 hours.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;visits_history&lt;/code&gt; stores the history of customer visits to various restaurants, accumulated over an indefinite time span.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Each night, you merge the daily visit count from &lt;code&gt;visits_daily&lt;/code&gt; into &lt;code&gt;visits_history&lt;/code&gt;. The merge operation modifies the target table in two ways:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Updates existing customer data.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Inserts new rows of data for first-time customers.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;One &lt;code&gt;MERGE&lt;/code&gt; statement executes both operations as a single (upsert) transaction.&lt;/p&gt;
&lt;h2 id=&#34;source-and-target-tables&#34;&gt;Source and target tables&lt;/h2&gt;
&lt;p&gt;The source and target tables &lt;code&gt;visits_daily&lt;/code&gt; and &lt;code&gt;visits_history&lt;/code&gt; are defined as follows:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;CREATE TABLE public.visits_daily
(
    customer_id int,
    location_name varchar(20),
    visit_time time(0) DEFAULT (now())::timetz(6)
);

CREATE TABLE public.visits_history
(
    customer_id int,
    location_name varchar(20),
    visit_count int
);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Table &lt;code&gt;visits_history&lt;/code&gt; contains rows of three customers who between them visited two restaurants, Etoile and LaRosa:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM visits_history ORDER BY customer_id, location_name;
 customer_id | location_name | visit_count
-------------+---------------+-------------
        1001 | Etoile        |           2
        1002 | La Rosa       |           4
        1004 | Etoile        |           1
(3 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;By close of business, table &lt;code&gt;visits_daily&lt;/code&gt; contains three rows of restaurant visits:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM visits_daily ORDER BY customer_id, location_name;
 customer_id | location_name | visit_time
-------------+---------------+------------
        1001 | Etoile        | 18:19:29
        1003 | Lux Cafe      | 08:07:00
        1004 | La Rosa       | 11:49:20
(3 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;table-data-merge&#34;&gt;Table data merge&lt;/h2&gt;
&lt;p&gt;The following &lt;code&gt;MERGE&lt;/code&gt; statement merges &lt;code&gt;visits_daily&lt;/code&gt; data into &lt;code&gt;visits_history&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;For matching customers, &lt;code&gt;MERGE&lt;/code&gt; updates the occurrence count.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;For non-matching customers, &lt;code&gt;MERGE&lt;/code&gt; inserts new rows.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; MERGE INTO visits_history h USING visits_daily d
    ON (h.customer_id=d.customer_id AND h.location_name=d.location_name)
    WHEN MATCHED THEN UPDATE SET visit_count = h.visit_count  + 1
    WHEN NOT MATCHED THEN INSERT (customer_id, location_name, visit_count)
    VALUES (d.customer_id, d.location_name, 1);
 OUTPUT
--------
      3
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;MERGE&lt;/code&gt; returns the number of rows updated and inserted. In this case, the returned value specifies three updates and inserts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Customer &lt;code&gt;1001&lt;/code&gt;&#39;s third visit to Etoile&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;New customer &lt;code&gt;1003&lt;/code&gt;&#39;s first visit to new restaurant Lux Cafe&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Customer &lt;code&gt;1004&lt;/code&gt;&#39;s first visit to La Rosa&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you now query table &lt;code&gt;visits_history&lt;/code&gt;, the result set shows the merged (updated and inserted) data. Updated and new rows are highlighted:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;../../../../images/data-management/basic-merge-example.png&#34; alt=&#34;&#34;&gt;&lt;/p&gt;

      </description>
    </item>
    
    <item>
      <title>Admin: MERGE source options</title>
      <link>/en/admin/working-with-native-tables/merging-table-data/merge-source-options/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/admin/working-with-native-tables/merging-table-data/merge-source-options/</guid>
      <description>
        
        
        &lt;p&gt;A &lt;code&gt;MERGE&lt;/code&gt; operation joins the target table to one of the following data sources:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Another table&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;View&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Subquery result set&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;merging-from-table-and-view-data&#34;&gt;Merging from table and view data&lt;/h2&gt;
&lt;p&gt;You merge data from one table into another as follows:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;MERGE INTO &lt;span class=&#34;code-variable&#34;&gt;target-table&lt;/span&gt; USING { &lt;span class=&#34;code-variable&#34;&gt;source-table&lt;/span&gt; | &lt;span class=&#34;code-variable&#34;&gt;source-view&lt;/span&gt; } &lt;span class=&#34;code-variable&#34;&gt;join-condition
   matching-clause&lt;/span&gt;[ &lt;span class=&#34;code-variable&#34;&gt;matching-clause&lt;/span&gt; ]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If you specify a view, the database expands the view name to the query that it encapsulates, and uses the result set as the merge source data.&lt;/p&gt;
&lt;p&gt;For example, the VMart table &lt;code&gt;public.product_dimension&lt;/code&gt; contains current and discontinued products. You can move all discontinued products into a separate table &lt;code&gt;public.product_dimension_discontinued&lt;/code&gt;, as follows:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TABLE public.product_dimension_discontinued (
     product_key int,
     product_version int,
     sku_number char(32),
     category_description char(32),
     product_description varchar(128));

=&amp;gt; MERGE INTO product_dimension_discontinued tgt
     USING product_dimension src ON tgt.product_key = src.product_key
                                AND tgt.product_version = src.product_version
     WHEN NOT MATCHED AND src.discontinued_flag=&amp;#39;1&amp;#39; THEN INSERT VALUES
       (src.product_key,
        src.product_version,
        src.sku_number,
        src.category_description,
        src.product_description);
 OUTPUT
--------
   1186
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Source table &lt;code&gt;product_dimension&lt;/code&gt; uses two columns, &lt;code&gt;product_key&lt;/code&gt; and &lt;code&gt;product_version&lt;/code&gt;, to identify unique products. The &lt;code&gt;MERGE&lt;/code&gt; statement joins the source and target tables on these columns in order to return single instances of non-matching rows. The &lt;code&gt;WHEN NOT MATCHED&lt;/code&gt; clause includes a &lt;a href=&#34;../../../../en/admin/working-with-native-tables/merging-table-data/update-and-insert-filters/&#34;&gt;filter&lt;/a&gt; (&lt;code&gt;src.discontinued_flag=&#39;1&#39;&lt;/code&gt;), which reduces the result set to include only discontinued products. The remaining rows are inserted into target table &lt;code&gt;product_dimension_discontinued&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&#34;merging-from-a-subquery-result-set&#34;&gt;Merging from a subquery result set&lt;/h2&gt;
&lt;p&gt;You can merge into a table the result set that is returned by a subquery, as follows:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;MERGE INTO &lt;span class=&#34;code-variable&#34;&gt;target-table&lt;/span&gt; USING (&lt;span class=&#34;code-variable&#34;&gt;subquery&lt;/span&gt;) &lt;span class=&#34;code-variable&#34;&gt;sq-alias&lt;/span&gt; &lt;span class=&#34;code-variable&#34;&gt;join-condition
   &lt;/span&gt;&lt;span class=&#34;code-variable&#34;&gt;matching-clause&lt;/span&gt;[ &lt;span class=&#34;code-variable&#34;&gt;matching-clause&lt;/span&gt; ]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For example, the VMart table &lt;code&gt;public.product_dimension&lt;/code&gt; is defined as follows (DDL truncated):&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;CREATE TABLE public.product_dimension
(
    product_key int NOT NULL,
    product_version int NOT NULL,
    product_description varchar(128),
    sku_number char(32),
    ...
)
ALTER TABLE public.product_dimension
    ADD CONSTRAINT C_PRIMARY PRIMARY KEY (product_key, product_version) DISABLED;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Columns &lt;code&gt;product_key&lt;/code&gt; and &lt;code&gt;product_version&lt;/code&gt; comprise the table&#39;s primary key. You can modify this table so it contains a single column that concatenates the values of these two columns. This column can be used to uniquely identify each product, while also maintaining the original values from &lt;code&gt;product_key&lt;/code&gt; and &lt;code&gt;product_version&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;You populate the new column with a &lt;code&gt;MERGE&lt;/code&gt; statement that queries the other two columns:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; ALTER TABLE public.product_dimension ADD COLUMN product_ID numeric(8,2);
ALTER TABLE

=&amp;gt; MERGE INTO product_dimension tgt
     USING (SELECT (product_key||&amp;#39;.0&amp;#39;||product_version)::numeric(8,2) AS pid, sku_number
     FROM product_dimension) src
     ON tgt.product_key||&amp;#39;.0&amp;#39;||product_version::numeric=src.pid
     WHEN MATCHED THEN UPDATE SET product_ID = src.pid;
 OUTPUT
--------
  60000
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The following query verifies that the new column values correspond to the values in &lt;code&gt;product_key&lt;/code&gt; and &lt;code&gt;product_version&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT product_ID, product_key, product_version, product_description
   FROM product_dimension
   WHERE category_description = &amp;#39;Medical&amp;#39;
     AND product_description ILIKE &amp;#39;%diabetes%&amp;#39;
     AND discontinued_flag = 1 ORDER BY product_ID;
 product_ID | product_key | product_version |           product_description
------------+-------------+-----------------+-----------------------------------------
    5836.02 |        5836 |               2 | Brand #17487 diabetes blood testing kit
   14320.02 |       14320 |               2 | Brand #43046 diabetes blood testing kit
   18881.01 |       18881 |               1 | Brand #56743 diabetes blood testing kit
(3 rows)
&lt;/code&gt;&lt;/pre&gt;
      </description>
    </item>
    
    <item>
      <title>Admin: MERGE matching clauses</title>
      <link>/en/admin/working-with-native-tables/merging-table-data/merge-matching-clauses/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/admin/working-with-native-tables/merging-table-data/merge-matching-clauses/</guid>
      <description>
        
        
        &lt;p&gt;&lt;code&gt;MERGE&lt;/code&gt; supports one instance of the following matching clauses:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;#WHEN_MATCHED&#34;&gt;WHEN MATCHED THEN UPDATE SET&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;#WHEN_NOT_MATCHED&#34;&gt;WHEN NOT MATCHED THEN INSERT&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Each matching clause can specify an additional filter, as described in &lt;a href=&#34;../../../../en/admin/working-with-native-tables/merging-table-data/update-and-insert-filters/#&#34;&gt;Update and insert filters&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;WHEN_MATCHED&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;when-matched-then-update-set&#34;&gt;&lt;code&gt;WHEN MATCHED THEN UPDATE SET&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;Updates all target table rows that are joined to the source table, typically with data from the source table:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;WHEN MATCHED [ AND &lt;span class=&#34;code-variable&#34;&gt;&lt;a href=&#34;../../../../en/admin/working-with-native-tables/merging-table-data/update-and-insert-filters/#&#34;&gt;update-filter&lt;/a&gt;&lt;/span&gt; ] THEN UPDATE
   SET { &lt;span class=&#34;code-variable&#34;&gt;target-column&lt;/span&gt; = &lt;span class=&#34;code-variable&#34;&gt;expression&lt;/span&gt; }[,...]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The database can execute the join only on unique values in the source table&#39;s join column. If the source table&#39;s join column contains more than one matching value, the &lt;code&gt;MERGE&lt;/code&gt; statement returns with a run-time error.&lt;/p&gt;
&lt;p&gt;&lt;a name=&#34;WHEN_NOT_MATCHED&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;when-not-matched-then-insert&#34;&gt;&lt;code&gt;WHEN NOT MATCHED THEN INSERT&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;WHEN NOT MATCHED THEN INSERT&lt;/code&gt; inserts into the target table a new row for each source table row that is excluded from the join:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;WHEN NOT MATCHED [ AND &lt;span class=&#34;code-variable&#34;&gt;&lt;a href=&#34;../../../../en/admin/working-with-native-tables/merging-table-data/update-and-insert-filters/#&#34;&gt;insert-filter&lt;/a&gt;&lt;/span&gt; ] THEN INSERT
   [ ( &lt;span class=&#34;code-variable&#34;&gt;column-list&lt;/span&gt; ) ] VALUES ( &lt;span class=&#34;code-variable&#34;&gt;values-list&lt;/span&gt; )
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;em&gt;&lt;code&gt;column-list&lt;/code&gt;&lt;/em&gt; is a comma-delimited list of one or more target columns in the target table, listed in any order. &lt;code&gt;MERGE&lt;/code&gt; maps &lt;em&gt;&lt;code&gt;column-list&lt;/code&gt;&lt;/em&gt; columns to &lt;em&gt;&lt;code&gt;values-list&lt;/code&gt;&lt;/em&gt; values in the same order, and each column-value pair must be &lt;a href=&#34;../../../../en/sql-reference/data-types/data-type-coercion/&#34;&gt;compatible&lt;/a&gt;. If you omit &lt;em&gt;&lt;code&gt;column-list&lt;/code&gt;&lt;/em&gt;, the database maps &lt;em&gt;&lt;code&gt;values-list&lt;/code&gt;&lt;/em&gt; values to columns according to column order in the table definition.&lt;/p&gt;
&lt;p&gt;For example, given the following source and target table definitions:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;CREATE TABLE t1 (a int, b int, c int);
CREATE TABLE t2 (x int, y int, z int);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The following &lt;code&gt;WHEN NOT MATCHED&lt;/code&gt; clause implicitly sets the values of the target table columns &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, and &lt;code&gt;c&lt;/code&gt; in the newly inserted rows:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;MERGE INTO t1 USING t2 ON t1.a=t2.x
   WHEN NOT MATCHED THEN INSERT VALUES (t2.x, t2.y, t2.z);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In contrast, the following &lt;code&gt;WHEN NOT MATCHED&lt;/code&gt; clause excludes columns &lt;code&gt;t1.b&lt;/code&gt; and &lt;code&gt;t2.y&lt;/code&gt; from the merge operation. The &lt;code&gt;WHEN NOT MATCHED&lt;/code&gt; clause explicitly pairs two sets of columns from the target and source tables: &lt;code&gt;t1.a&lt;/code&gt; to &lt;code&gt;t2.x&lt;/code&gt;, and &lt;code&gt;t1.c&lt;/code&gt; to &lt;code&gt;t2.z&lt;/code&gt;. The database sets excluded column &lt;code&gt;t1.b&lt;/code&gt;. to null:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;MERGE INTO t1 USING t2 ON t1.a=t2.x
   WHEN NOT MATCHED THEN INSERT (a, c) VALUES (t2.x, t2.z);
&lt;/code&gt;&lt;/pre&gt;
      </description>
    </item>
    
    <item>
      <title>Admin: Update and insert filters</title>
      <link>/en/admin/working-with-native-tables/merging-table-data/update-and-insert-filters/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/admin/working-with-native-tables/merging-table-data/update-and-insert-filters/</guid>
      <description>
        
        
        &lt;p&gt;Each &lt;code&gt;WHEN MATCHED&lt;/code&gt; and &lt;code&gt;WHEN NOT MATCHED&lt;/code&gt; clause in a &lt;code&gt;MERGE&lt;/code&gt; statement can optionally specify an update filter and insert filter, respectively:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;WHEN MATCHED &lt;span class=&#34;code-input&#34;&gt;AND &lt;/span&gt;&lt;span class=&#34;code-variable&#34;&gt;update-filter&lt;/span&gt; THEN UPDATE ...
WHEN NOT MATCHED &lt;span class=&#34;code-input&#34;&gt;AND &lt;/span&gt;&lt;span class=&#34;code-variable&#34;&gt;insert-filter&lt;/span&gt; THEN INSERT ...
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The database also supports Oracle syntax for specifying update and insert filters:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;WHEN MATCHED THEN UPDATE SET &lt;span class=&#34;code-variable&#34;&gt;column-updates&lt;/span&gt; &lt;span class=&#34;code-input&#34;&gt;WHERE &lt;/span&gt;&lt;span class=&#34;code-variable&#34;&gt;update-filter&lt;/span&gt;
WHEN NOT MATCHED THEN INSERT &lt;span class=&#34;code-variable&#34;&gt;column-values&lt;/span&gt; &lt;span class=&#34;code-input&#34;&gt;WHERE &lt;/span&gt;&lt;span class=&#34;code-variable&#34;&gt;insert-filter&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Each filter can specify multiple conditions. The database handles the filters as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;An update filter is applied to the set of matching rows in the target table that are returned by the &lt;code&gt;MERGE&lt;/code&gt; join. For each row where the update filter evaluates to true, the database updates the specified columns.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;An insert filter is applied to the set of source table rows that are excluded from the &lt;code&gt;MERGE&lt;/code&gt; join. For each row where the insert filter evaluates to true, the database adds a new row to the target table with the specified values.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For example, given the following data in tables &lt;code&gt;t11&lt;/code&gt; and &lt;code&gt;t22&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;
=&amp;gt; SELECT * from t11 ORDER BY pk;
 pk | col1 | col2 | SKIP_ME_FLAG
----+------+------+--------------
  1 |    2 |    3 | t
  2 |    3 |    4 | t
  3 |    4 |    5 | f
  4 |      |    6 | f
  5 |    6 |    7 | t
  6 |      |    8 | f
  7 |    8 |      | t
(7 rows)

=&amp;gt; SELECT * FROM t22 ORDER BY pk;
 pk | col1 | col2
----+------+------
  1 |    2 |    4
  2 |    4 |    8
  3 |    6 |
  4 |    8 |   16
(4 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can merge data from table &lt;code&gt;t11&lt;/code&gt; into table &lt;code&gt;t22&lt;/code&gt; with the following &lt;code&gt;MERGE&lt;/code&gt; statement, which includes update and insert filters:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; MERGE INTO t22 USING t11 ON ( t11.pk=t22.pk )
   WHEN MATCHED
       AND t11.SKIP_ME_FLAG=FALSE AND (
         COALESCE (t22.col1&amp;lt;&amp;gt;t11.col1, (t22.col1 is null)&amp;lt;&amp;gt;(t11.col1 is null))
       )
   THEN UPDATE SET col1=t11.col1, col2=t11.col2
   WHEN NOT MATCHED
      AND t11.SKIP_ME_FLAG=FALSE
   THEN INSERT (pk, col1, col2) VALUES (t11.pk, t11.col1, t11.col2);
 OUTPUT
--------
      3
(1 row)

=&amp;gt; SELECT * FROM t22 ORDER BY pk;
 pk | col1 | col2
----+------+------
  1 |    2 |    4
  2 |    4 |    8
  3 |    4 |    5
  4 |      |    6
  6 |      |    8
(5 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The database uses the update and insert filters as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Evaluates all matching rows against the update filter conditions. The database updates each row where the following two conditions both evaluate to true:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Source column &lt;code&gt;t11.SKIP_ME_FLAG&lt;/code&gt; is set to false.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;COALESCE&lt;/code&gt; function evaluates to true.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Evaluates all non-matching rows in the source table against the insert filter. For each row where column &lt;code&gt;t11.SKIP_ME_FLAG&lt;/code&gt; is set to false, the database inserts a new row in the target table.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
    <item>
      <title>Admin: MERGE optimization</title>
      <link>/en/admin/working-with-native-tables/merging-table-data/merge-optimization/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/admin/working-with-native-tables/merging-table-data/merge-optimization/</guid>
      <description>
        
        
        &lt;p&gt;You can improve &lt;code&gt;MERGE&lt;/code&gt; performance in several ways:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;#Projecti&#34;&gt;Design projections for optimal &lt;code&gt;MERGE&lt;/code&gt; performance&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;#Requirem&#34;&gt;Facilitate creation of optimized query plans&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use source tables that are smaller than target tables.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;Projecti&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;projections-for-merge-operations&#34;&gt;Projections for MERGE operations&lt;/h2&gt;
&lt;p&gt;The database query optimizer automatically chooses the best projections to implement a merge operation. A good projection design strategy provides projections that help the query optimizer avoid extra sort and data transfer operations, and facilitate &lt;code&gt;MERGE&lt;/code&gt; performance.

&lt;div class=&#34;alert admonition tip&#34; role=&#34;alert&#34;&gt;
&lt;h4 class=&#34;admonition-head&#34;&gt;Tip&lt;/h4&gt;

You can rely on &lt;a class=&#34;glosslink&#34; href=&#34;../../../../en/glossary/db-designer/&#34; title=&#34;A tool that analyzes a logical schema definition, sample queries, and sample data, and creates a physical schema () in the form of a SQL script that you deploy automatically or manually.&#34;&gt;Database Designer&lt;/a&gt; to generate projections that address merge requirements. You can then &lt;a href=&#34;../../../../en/admin/configuring-db/creating-db-design/creating-custom-designs/custom-design-process/&#34;&gt;customize these projections&lt;/a&gt; as needed.

&lt;/div&gt;&lt;/p&gt;
&lt;p&gt;For example, the following &lt;code&gt;MERGE&lt;/code&gt; statement fragment joins source and target tables &lt;code&gt;tgt&lt;/code&gt; and &lt;code&gt;src&lt;/code&gt;, respectively, on columns &lt;code&gt;tgt.a&lt;/code&gt; and &lt;code&gt;src.b&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; MERGE INTO tgt USING src ON tgt.a = src.b ...
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The database can use a local merge join if projections for tables &lt;code&gt;tgt&lt;/code&gt; and &lt;code&gt;src&lt;/code&gt; use one of the following projection designs, where inputs are presorted by projection &lt;code&gt;ORDER BY&lt;/code&gt; clauses:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Replicated projections are sorted on:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Column &lt;code&gt;a&lt;/code&gt; for table &lt;code&gt;tgt&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Column &lt;code&gt;b&lt;/code&gt; for table &lt;code&gt;src&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class=&#34;glosslink&#34; href=&#34;../../../../en/glossary/segmentation/&#34; title=&#34;Defines how physical data storage (projections) is stored in a database cluster using the CREATE PROJECTION statement.&#34;&gt;Segmented&lt;/a&gt; projections are &lt;a href=&#34;../../../../en/data-analysis/query-optimization/join-queries/identical-segmentation/&#34;&gt;identically segmented&lt;/a&gt; on:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Column &lt;code&gt;a&lt;/code&gt; for table &lt;code&gt;tgt&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Column &lt;code&gt;b&lt;/code&gt; for table &lt;code&gt;src&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Corresponding segmented columns&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a name=&#34;Requirem&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&#34;optimizing-merge-query-plans&#34;&gt;Optimizing MERGE query plans&lt;/h2&gt;
&lt;p&gt;The database prepares an optimized query plan if the following conditions are all true:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;MERGE&lt;/code&gt; statement contains both matching clauses 
&lt;code&gt;&lt;a href=&#34;../../../../en/admin/working-with-native-tables/merging-table-data/merge-matching-clauses/#&#34;&gt;WHEN MATCHED THEN UPDATE SET&lt;/a&gt;&lt;/code&gt; and 
&lt;code&gt;&lt;a href=&#34;../../../../en/admin/working-with-native-tables/merging-table-data/merge-matching-clauses/#&#34;&gt;WHEN NOT MATCHED THEN INSERT&lt;/a&gt;&lt;/code&gt;. If the &lt;code&gt;MERGE&lt;/code&gt; statement contains only one matching clause, it uses a non-optimized query plan.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;MERGE&lt;/code&gt; statement excludes &lt;a href=&#34;../../../../en/admin/working-with-native-tables/merging-table-data/update-and-insert-filters/&#34;&gt;update and insert filters&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The target table join column has a unique or primary key constraint. This requirement does not apply to the source table join column.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Both matching clauses specify all columns in the target table.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Both matching clauses specify identical source values.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For details on evaluating an 
&lt;code&gt;&lt;a href=&#34;../../../../en/sql-reference/statements/explain/#&#34;&gt;EXPLAIN&lt;/a&gt;&lt;/code&gt;-generated query plan, see &lt;a href=&#34;../../../../en/admin/managing-queries/query-plans/query-plan-information-and-structure/merge-path/#&#34;&gt;MERGE path&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The examples that follow use a simple schema to illustrate some of the conditions under which the database prepares or does not prepare an optimized query plan for &lt;code&gt;MERGE&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;CREATE TABLE target(a INT PRIMARY KEY, b INT, c INT) ORDER BY b,a;
CREATE TABLE source(a INT, b INT, c INT) ORDER BY b,a;
INSERT INTO target VALUES(1,2,3);
INSERT INTO target VALUES(2,4,7);
INSERT INTO source VALUES(3,4,5);
INSERT INTO source VALUES(4,6,9);
COMMIT;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Optimized MERGE statement&lt;/p&gt;
&lt;p&gt;The database can prepare an optimized query plan for the following &lt;code&gt;MERGE&lt;/code&gt; statement because:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The target table&#39;s join column &lt;code&gt;t.a&lt;/code&gt; has a primary key constraint.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;All columns in the target table &lt;code&gt;(a,b,c)&lt;/code&gt; are included in the &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;INSERT&lt;/code&gt; clauses.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;INSERT&lt;/code&gt; clauses specify identical source values: &lt;code&gt;s.a&lt;/code&gt;, &lt;code&gt;s.b&lt;/code&gt;, and &lt;code&gt;s.c&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;MERGE INTO target t USING source s ON t.a = s.a
  WHEN MATCHED THEN UPDATE SET a=s.a, b=s.b, c=s.c
  WHEN NOT MATCHED THEN INSERT(a,b,c) VALUES(s.a, s.b, s.c);

 OUTPUT
--------
2
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The output value of 2 indicates success and denotes the number of rows updated/inserted from the source into the target.&lt;/p&gt;
&lt;p&gt;Non-optimized MERGE statement&lt;/p&gt;
&lt;p&gt;In the next example, the &lt;code&gt;MERGE&lt;/code&gt; statement runs without optimization because the source values in the &lt;code&gt;UPDATE/INSERT&lt;/code&gt; clauses are not identical. Specifically, the &lt;code&gt;UPDATE&lt;/code&gt; clause includes constants for columns &lt;code&gt;s.a&lt;/code&gt; and &lt;code&gt;s.c&lt;/code&gt; and the &lt;code&gt;INSERT&lt;/code&gt; clause does not:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;
MERGE INTO target t USING source s ON t.a = s.a
  WHEN MATCHED THEN UPDATE SET a=s.a + 1, b=s.b, c=s.c - 1
  WHEN NOT MATCHED THEN INSERT(a,b,c) VALUES(s.a, s.b, s.c);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To make the previous &lt;code&gt;MERGE&lt;/code&gt; statement eligible for optimization, rewrite the statement so that the source values in the &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;INSERT&lt;/code&gt; clauses are identical:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;
MERGE INTO target t USING source s ON t.a = s.a
  WHEN MATCHED THEN UPDATE SET a=s.a + 1, b=s.b, c=s.c -1
  WHEN NOT MATCHED THEN INSERT(a,b,c) VALUES(s.a + 1, s.b, s.c - 1);
&lt;/code&gt;&lt;/pre&gt;
      </description>
    </item>
    
    <item>
      <title>Admin: MERGE restrictions</title>
      <link>/en/admin/working-with-native-tables/merging-table-data/merge-restrictions/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/admin/working-with-native-tables/merging-table-data/merge-restrictions/</guid>
      <description>
        
        
        &lt;p&gt;The following restrictions apply to updating and inserting table data with 
&lt;code&gt;&lt;a href=&#34;../../../../en/sql-reference/statements/merge/#&#34;&gt;MERGE&lt;/a&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&#34;constraint-enforcement&#34;&gt;Constraint enforcement&lt;/h2&gt;
&lt;p&gt;If primary key, unique key, or check constraints are enabled for automatic enforcement in the target table, the database enforces those constraints when you load new data. If a violation occurs, the database rolls back the operation and returns an error.

&lt;div class=&#34;admonition caution&#34; role=&#34;alert&#34;&gt;
&lt;h4 class=&#34;admonition-head&#34;&gt;Caution&lt;/h4&gt;

If you run &lt;span class=&#34;sql&#34;&gt;MERGE&lt;/span&gt; multiple times using the same target and source table, each iteration is liable to introduce duplicate values into the target columns and return with an error.

&lt;/div&gt;&lt;/p&gt;
&lt;h2 id=&#34;columns-prohibited-from-merge&#34;&gt;Columns prohibited from merge&lt;/h2&gt;
&lt;p&gt;The following columns cannot be specified in a merge operation; attempts to do so return with an error:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;../../../../en/admin/working-with-native-tables/sequences/identity-sequences/&#34;&gt;IDENTITY&lt;/a&gt; columns, or columns whose default value is set to a &lt;a href=&#34;../../../../en/admin/working-with-native-tables/sequences/named-sequences/creating-and-using-named-sequences/&#34;&gt;named sequence&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Vmap columns such as &lt;code&gt;__raw__&lt;/code&gt; in flex tables.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Columns of complex types ARRAY, SET, or ROW.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;


      </description>
    </item>
    
  </channel>
</rss>
