バージョンごとのドキュメント一覧

MERGE

MERGE <refpurpose>conditionally insert, update, or delete rows of a table</refpurpose> — テーブルの行を条件付きでINSERT、UPDATE、DELETEする

概要

[ WITH with_query [, ...] ]
MERGE INTO [ ONLY ] target_table_name [ * ] [ [ AS ] target_alias ]
USING data_source ON join_condition
when_clause [...]

where data_source is:

{ [ ONLY ] source_table_name [ * ] | ( source_query ) } [ [ AS ] source_alias ]

and when_clause is:

{ WHEN MATCHED [ AND condition ] THEN { merge_update | merge_delete | DO NOTHING } |
  WHEN NOT MATCHED [ AND condition ] THEN { merge_insert | DO NOTHING } }

and merge_insert is:

INSERT [( column_name [, ...] )]
[ OVERRIDING { SYSTEM | USER } VALUE ]
{ VALUES ( { expression | DEFAULT } [, ...] ) | DEFAULT VALUES }

and merge_update is:

UPDATE SET { column_name = { expression | DEFAULT } |
             ( column_name [, ...] ) = ( { expression | DEFAULT } [, ...] ) } [, ...]

and merge_delete is:

DELETE

説明

<title>Description</title>

<command>MERGE</command> performs actions that modify rows in the <replaceable class="parameter">target_table_name</replaceable>, using the <replaceable class="parameter">data_source</replaceable>. <command>MERGE</command> provides a single <acronym>SQL</acronym> statement that can conditionally <command>INSERT</command>, <command>UPDATE</command> or <command>DELETE</command> rows, a task that would otherwise require multiple procedural language statements. MERGEは、data_sourceを使用して、target_table_nameの行を変更するアクションを実行します。 MERGEは、条件付きでINSERTUPDATEまたはDELETE行を使用できる単一のSQL文を提供します。 この処理を行わないと、複数の手続き言語文が必要になります。

First, the <command>MERGE</command> command performs a join from <replaceable class="parameter">data_source</replaceable> to <replaceable class="parameter">target_table_name</replaceable> producing zero or more candidate change rows. For each candidate change row, the status of <literal>MATCHED</literal> or <literal>NOT MATCHED</literal> is set just once, after which <literal>WHEN</literal> clauses are evaluated in the order specified. For each candidate change row, the first clause to evaluate as true is executed. No more than one <literal>WHEN</literal> clause is executed for any candidate change row. 最初に、MERGEコマンドはdata_sourceからtarget_table_nameへの結合を実行し、0以上の候補変更行を生成します。 各候補変更行に対して、MATCHEDまたはNOT MATCHEDのステータスが一度だけ設定され、その後WHEN句が指定された順序で評価されます。 各候補変更行に対して、真と評価される最初の句が実行されます。 どの候補変更行に対しても、1つのWHEN句しか実行されません。

<command>MERGE</command> actions have the same effect as regular <command>UPDATE</command>, <command>INSERT</command>, or <command>DELETE</command> commands of the same names. The syntax of those commands is different, notably that there is no <literal>WHERE</literal> clause and no table name is specified. All actions refer to the <replaceable class="parameter">target_table_name</replaceable>, though modifications to other tables may be made using triggers. MERGEアクションは、同じ名前の通常のUPDATEINSERTまたはDELETEコマンドと同じ効果を持ちます。 これらのコマンドの構文は異なり、WHERE句がなく、テーブル名が指定されていません。 すべてのアクションはtarget_table_nameを参照しますが、他のテーブルへの変更はトリガを使用して行うことができます。

When <literal>DO NOTHING</literal> is specified, the source row is skipped. Since actions are evaluated in their specified order, <literal>DO NOTHING</literal> can be handy to skip non-interesting source rows before more fine-grained handling. DO NOTHINGが指定されている場合、ソース行はスキップされます。 アクションは指定された順序で評価されるため、DO NOTHINGは、より詳細な処理の前に、関心のないソース行をスキップする場合に便利です。

There is no separate <literal>MERGE</literal> privilege. If you specify an update action, you must have the <literal>UPDATE</literal> privilege on the column(s) of the <replaceable class="parameter">target_table_name</replaceable> that are referred to in the <literal>SET</literal> clause. If you specify an insert action, you must have the <literal>INSERT</literal> privilege on the <replaceable class="parameter">target_table_name</replaceable>. If you specify an delete action, you must have the <literal>DELETE</literal> privilege on the <replaceable class="parameter">target_table_name</replaceable>. Privileges are tested once at statement start and are checked whether or not particular <literal>WHEN</literal> clauses are executed. You will require the <literal>SELECT</literal> privilege on the <replaceable class="parameter">data_source</replaceable> and any column(s) of the <replaceable class="parameter">target_table_name</replaceable> referred to in a <literal>condition</literal>. 個別のMERGE権限はありません。 更新アクションを指定する場合は、SET句で参照されるtarget_table_nameの列に対してUPDATE権限を持っている必要があります。 挿入アクションを指定する場合は、target_table_nameに対してINSERT権限を持っている必要があります。 削除アクションを指定する場合は、target_table_nameに対してDELETE権限を持っている必要があります。 権限は文の開始時に一度テストされ、特定のWHEN句が実行されたかどうかがチェックされます。 data_sourceおよびconditionで参照されるtarget_table_nameのすべての列に対してSELECT権限が必要です。

<command>MERGE</command> is not supported if the <replaceable class="parameter">target_table_name</replaceable> is a materialized view, foreign table, or if it has any rules defined on it. target_table_nameが実体化ビュー(Materialized View)、外部テーブルである場合、またはテーブルに規則が定義されている場合、MERGEはサポートされません。

パラメータ

<title>Parameters</title>
target_table_name

The name (optionally schema-qualified) of the target table to merge into. If <literal>ONLY</literal> is specified before the table name, matching rows are updated or deleted in the named table only. If <literal>ONLY</literal> is not specified, matching rows are also updated or deleted in any tables inheriting from the named table. Optionally, <literal>*</literal> can be specified after the table name to explicitly indicate that descendant tables are included. The <literal>ONLY</literal> keyword and <literal>*</literal> option do not affect insert actions, which always insert into the named table only. マージ先のターゲットテーブルの名前です(スキーマ修飾名も可)。 テーブル名の前にONLYを指定すると、指定したテーブルでのみ一致する行が更新または削除されます。 ONLYを指定しないと、指定したテーブルを継承するテーブルでも一致する行が更新または削除されます。 オプションで、テーブル名の後に*を指定して、子孫のテーブルが含まれることを明示的に示すことができます。 ONLYキーワードおよび*オプションは、挿入操作には影響しません。 挿入操作では、常に指定したテーブルにのみ挿入します。

target_alias

A substitute name for the target table. When an alias is provided, it completely hides the actual name of the table. For example, given <literal>MERGE INTO foo AS f</literal>, the remainder of the <command>MERGE</command> statement must refer to this table as <literal>f</literal> not <literal>foo</literal>. ターゲットテーブルの代替名です。 別名を指定すると、テーブルの実際の名前が完全に非表示になります。 たとえば、MERGE INTO foo AS fを指定した場合、MERGE文の残りの部分は、このテーブルをfooではなくfとして参照する必要があります。

source_table_name

The name (optionally schema-qualified) of the source table, view, or transition table. If <literal>ONLY</literal> is specified before the table name, matching rows are included from the named table only. If <literal>ONLY</literal> is not specified, matching rows are also included from any tables inheriting from the named table. Optionally, <literal>*</literal> can be specified after the table name to explicitly indicate that descendant tables are included. ソーステーブル、ビュー、または遷移テーブルの名前(スキーマ修飾名も可)。 テーブル名の前にONLYを指定すると、指定したテーブルのみからの一致する行が含まれます。 ONLYを指定しないと、指定したテーブルを継承するすべてのテーブルからも一致する行が含まれます。 オプションで、テーブル名の後に*を指定して、子孫のテーブルが含まれることを明示的に示すことができます。

source_query

A query (<command>SELECT</command> statement or <command>VALUES</command> statement) that supplies the rows to be merged into the <replaceable class="parameter">target_table_name</replaceable>. Refer to the <xref linkend="sql-select"/> statement or <xref linkend="sql-values"/> statement for a description of the syntax. target_table_nameにマージされる行を提供する問合せ(SELECT文またはVALUES文)です。 構文の説明は、SELECT文またはVALUES文を参照してください。

source_alias

A substitute name for the data source. When an alias is provided, it completely hides the actual name of the table or the fact that a query was issued. データソースの代替名です。 別名を指定すると、テーブルの実際の名前やクエリが発行された事実が完全に非表示になります。

join_condition

<replaceable class="parameter">join_condition</replaceable> is an expression resulting in a value of type <type>boolean</type> (similar to a <literal>WHERE</literal> clause) that specifies which rows in the <replaceable class="parameter">data_source</replaceable> match rows in the <replaceable class="parameter">target_table_name</replaceable>. join_conditionboolean型の値を返す式です(WHERE句に似ています)。 この式は、data_sourceのどの行がtarget_table_nameの行と一致するかを指定します。

警告

Only columns from <replaceable class="parameter">target_table_name</replaceable> that attempt to match <replaceable class="parameter">data_source</replaceable> rows should appear in <replaceable class="parameter">join_condition</replaceable>. <replaceable class="parameter">join_condition</replaceable> subexpressions that only reference <replaceable class="parameter">target_table_name</replaceable> columns can affect which action is taken, often in surprising ways. join_conditionには、data_source行に一致しようとするtarget_table_nameの列のみが表示されます。 target_table_name列のみを参照するjoin_conditionサブ式は、実行されるアクションに影響を与える可能性があり、多くの場合驚くべき方法で影響を与えます。

when_clause

At least one <literal>WHEN</literal> clause is required. 少なくとも1つのWHEN句が必要です。

If the <literal>WHEN</literal> clause specifies <literal>WHEN MATCHED</literal> and the candidate change row matches a row in the <replaceable class="parameter">target_table_name</replaceable>, the <literal>WHEN</literal> clause is executed if the <replaceable class="parameter">condition</replaceable> is absent or it evaluates to <literal>true</literal>. WHEN句でWHEN MATCHEDが指定され、変更候補行がtarget_table_nameの行と一致する場合、conditionが存在しないかと評価されるとWHEN句が実行されます。

Conversely, if the <literal>WHEN</literal> clause specifies <literal>WHEN NOT MATCHED</literal> and the candidate change row does not match a row in the <replaceable class="parameter">target_table_name</replaceable>, the <literal>WHEN</literal> clause is executed if the <replaceable class="parameter">condition</replaceable> is absent or it evaluates to <literal>true</literal>. 逆に、WHEN句がWHEN NOT MATCHEDを指定し、変更候補行がtarget_table_nameの行と一致しない場合、conditionが存在しないか、と評価されたときにWHEN句が実行されます。

condition

An expression that returns a value of type <type>boolean</type>. If this expression for a <literal>WHEN</literal> clause returns <literal>true</literal>, then the action for that clause is executed for that row. boolean型の値を返す式。 WHEN句のこの式がを返す場合、その句のアクションがその行に対して実行されます。

A condition on a <literal>WHEN MATCHED</literal> clause can refer to columns in both the source and the target relations. A condition on a <literal>WHEN NOT MATCHED</literal> clause can only refer to columns from the source relation, since by definition there is no matching target row. Only the system attributes from the target table are accessible. WHEN MATCHED句の条件は、ソースリレーションとターゲットリレーションの両方の列を参照できます。 WHEN NOT MATCHED句の条件は、ソースリレーションの列のみを参照できます。 これは、定義上、一致するターゲット行がないためです。 ターゲットテーブルのシステム属性のみにアクセスできます。

merge_insert

The specification of an <literal>INSERT</literal> action that inserts one row into the target table. The target column names can be listed in any order. If no list of column names is given at all, the default is all the columns of the table in their declared order. ターゲットテーブルに1つの行を挿入するINSERTアクションの指定。 ターゲット列名は任意の順序でリストできます。 列名のリストがまったく指定されていない場合、デフォルトではテーブルのすべての列が宣言された順序になります。

Each column not present in the explicit or implicit column list will be filled with a default value, either its declared default value or null if there is none. 明示的または暗黙的な列リストにない各列にはデフォルト値(デフォルト値が宣言されていればその値、未宣言ならばNULL)が挿入されます。

If <replaceable class="parameter">target_table_name</replaceable> is a partitioned table, each row is routed to the appropriate partition and inserted into it. If <replaceable class="parameter">target_table_name</replaceable> is a partition, an error will occur if any input row violates the partition constraint. target_table_nameがパーティションテーブルの場合、各行は適切なパーティションにルーティングされて挿入されます。 target_table_nameがパーティションの場合、入力行がパーティション制約に違反するとエラーが発生します。

Column names may not be specified more than once. <command>INSERT</command> actions cannot contain sub-selects. 列名を複数回指定することはできません。 INSERTアクションに副選択を含めることはできません。

Only one <literal>VALUES</literal> clause can be specified. The <literal>VALUES</literal> clause can only refer to columns from the source relation, since by definition there is no matching target row. VALUES句は1つしか指定できません。 VALUES句はソースリレーションの列のみを参照できます。 これは、定義上、一致するターゲット行がないためです。

merge_update

The specification of an <literal>UPDATE</literal> action that updates the current row of the <replaceable class="parameter">target_table_name</replaceable>. Column names may not be specified more than once. target_table_nameの現在の行を更新するUPDATEアクションの指定。 列名は2回以上指定できません。

Neither a table name nor a <literal>WHERE</literal> clause are allowed. テーブル名もWHERE句も使用できません。

merge_delete

Specifies a <literal>DELETE</literal> action that deletes the current row of the <replaceable class="parameter">target_table_name</replaceable>. Do not include the table name or any other clauses, as you would normally do with a <xref linkend="sql-delete"/> command. target_table_nameの現在の行を削除するDELETEアクションを指定します。 DELETEコマンドで通常行うように、テーブル名やその他の句は含めないでください。

column_name

The name of a column in the <replaceable class="parameter">target_table_name</replaceable>. The column name can be qualified with a subfield name or array subscript, if needed. (Inserting into only some fields of a composite column leaves the other fields null.) Do not include the table's name in the specification of a target column. target_table_name内の列名。 列名は、必要に応じてサブフィールド名または配列の添字で修飾できます。 (複合列の一部のフィールドにのみ挿入すると、他のフィールドはNULLになります。) ターゲット列の指定には、テーブルの名前を含めないでください。

OVERRIDING SYSTEM VALUE

Without this clause, it is an error to specify an explicit value (other than <literal>DEFAULT</literal>) for an identity column defined as <literal>GENERATED ALWAYS</literal>. This clause overrides that restriction. この句を使用しない場合、GENERATED ALWAYSとして定義されたID列に対して明示的な値(DEFAULT以外)を指定するとエラーになります。 この句は、この制限を上書きします。

OVERRIDING USER VALUE

If this clause is specified, then any values supplied for identity columns defined as <literal>GENERATED BY DEFAULT</literal> are ignored and the default sequence-generated values are applied. この句を指定した場合、GENERATED BY DEFAULTとして定義されたID列に提供された値は無視され、シーケンスで生成されたデフォルト値が適用されます。

DEFAULT VALUES

All columns will be filled with their default values. (An <literal>OVERRIDING</literal> clause is not permitted in this form.) すべての列にデフォルト値が設定されます(このフォームではOVERRIDING句は使用できません)。

expression

An expression to assign to the column. If used in a <literal>WHEN MATCHED</literal> clause, the expression can use values from the original row in the target table, and values from the <literal>data_source</literal> row. If used in a <literal>WHEN NOT MATCHED</literal> clause, the expression can use values from the <literal>data_source</literal>. 列に割り当てる式。 WHEN MATCHED句で使用する場合、式ではターゲットテーブルの元の行の値とdata_source行の値を使用できます。 WHEN NOT MATCHED句で使用する場合、式ではdata_sourceの値を使用できます。

DEFAULT

Set the column to its default value (which will be <literal>NULL</literal> if no specific default expression has been assigned to it). 列をデフォルト値に設定します(特定のデフォルト式が割り当てられていない場合はNULLになります)。

with_query

The <literal>WITH</literal> clause allows you to specify one or more subqueries that can be referenced by name in the <command>MERGE</command> query. See <xref linkend="queries-with"/> and <xref linkend="sql-select"/> for details. WITH句を使用すると、MERGEクエリで名前で参照できる1つ以上のサブクエリを指定できます。 詳細は7.8SELECTを参照してください。

出力

<title>Outputs</title>

On successful completion, a <command>MERGE</command> command returns a command tag of the form 正常に完了すると、MERGEコマンドは以下の形式のコマンドタグを返します。

MERGE total_count

The <replaceable class="parameter">total_count</replaceable> is the total number of rows changed (whether inserted, updated, or deleted). If <replaceable class="parameter">total_count</replaceable> is 0, no rows were changed in any way. total_countは変更された行の合計数です(挿入、更新、または削除のいずれか)。 total_countが0の場合、行はまったく変更されていません。

注釈

<title>Notes</title>

The following steps take place during the execution of <command>MERGE</command>. 次のステップは、MERGEの実行中に行われます。

  1. Perform any <literal>BEFORE STATEMENT</literal> triggers for all actions specified, whether or not their <literal>WHEN</literal> clauses match. WHEN句が一致するかどうかに関係なく、指定されたすべてのアクションに対してBEFORE STATEMENTトリガを実行します。

  2. Perform a join from source to target table. The resulting query will be optimized normally and will produce a set of candidate change rows. For each candidate change row, <orderedlist> <listitem> <para> Evaluate whether each row is <literal>MATCHED</literal> or <literal>NOT MATCHED</literal>. ソーステーブルからターゲットテーブルへの結合を実行します。 結果の問合せは通常どおり最適化され、一連の候補変更行が生成されます。 候補変更行ごとに、

    1. 各行がMATCHEDまたはNOT MATCHEDであるかどうかを評価します。

    2. Test each <literal>WHEN</literal> condition in the order specified until one returns true. 真が返されるまで、各WHEN条件を指定された順序でテストします。

    3. When a condition returns true, perform the following actions: 条件が真を返す場合は、次のアクションを実行します。

      1. Perform any <literal>BEFORE ROW</literal> triggers that fire for the action's event type. アクションのイベントタイプに対して起動するBEFORE ROWトリガを実行します。

      2. Perform the specified action, invoking any check constraints on the target table. 指定されたアクションを実行し、ターゲットテーブルの検査制約を呼び出します。

      3. Perform any <literal>AFTER ROW</literal> triggers that fire for the action's event type. アクションのイベントタイプに対して起動するAFTER ROWトリガを実行します。

  3. Perform any <literal>AFTER STATEMENT</literal> triggers for actions specified, whether or not they actually occur. This is similar to the behavior of an <command>UPDATE</command> statement that modifies no rows. アクションが実際に発生するかどうかに関係なく、指定されたアクションに対してAFTER STATEMENTトリガを実行します。 これは、行を変更しないUPDATE文の動作に似ています。

In summary, statement triggers for an event type (say, <command>INSERT</command>) will be fired whenever we <emphasis>specify</emphasis> an action of that kind. In contrast, row-level triggers will fire only for the specific event type being <emphasis>executed</emphasis>. So a <command>MERGE</command> command might fire statement triggers for both <command>UPDATE</command> and <command>INSERT</command>, even though only <command>UPDATE</command> row triggers were fired. 要約するとイベントタイプの文トリガ(たとえば、INSERTなど)は、その種類のアクションを指定 するたびに起動されます。 対照的に、行レベルトリガは、実行される特定のイベントタイプに対してのみ起動されます。 したがって、MERGEコマンドでは、UPDATE行トリガのみが起動された場合でも、UPDATEINSERTの両方に対して文トリガを起動する可能性があります。

You should ensure that the join produces at most one candidate change row for each target row. In other words, a target row shouldn't join to more than one data source row. If it does, then only one of the candidate change rows will be used to modify the target row; later attempts to modify the row will cause an error. This can also occur if row triggers make changes to the target table and the rows so modified are then subsequently also modified by <command>MERGE</command>. If the repeated action is an <command>INSERT</command>, this will cause a uniqueness violation, while a repeated <command>UPDATE</command> or <command>DELETE</command> will cause a cardinality violation; the latter behavior is required by the <acronym>SQL</acronym> standard. This differs from historical <productname>PostgreSQL</productname> behavior of joins in <command>UPDATE</command> and <command>DELETE</command> statements where second and subsequent attempts to modify the same row are simply ignored. 結合では、各ターゲット行に対して最大1つの候補変更行が生成されるようにする必要があります。 つまり、ターゲット行は複数のデータソース行に結合できません。 結合する場合、候補変更行の1つだけがターゲット行の変更に使用されます。 後で行を変更しようとするとエラーが発生します。 これは、行トリガがターゲットテーブルを変更し、変更された行が後でMERGEによっても変更される場合にも発生する可能性があります。 繰り返されるアクションがINSERTの場合、一意性違反が発生しますが、UPDATEまたはDELETEを繰り返すとカーディナリティ違反が発生します。 後者の動作はSQL標準で要求されています。 これは、PostgreSQLUPDATEおよびDELETE文における結合の歴史的な動作とは異なります。 この動作では、2回目以降の同じ行の変更は単純に無視されます。

If a <literal>WHEN</literal> clause omits an <literal>AND</literal> sub-clause, it becomes the final reachable clause of that kind (<literal>MATCHED</literal> or <literal>NOT MATCHED</literal>). If a later <literal>WHEN</literal> clause of that kind is specified it would be provably unreachable and an error is raised. If no final reachable clause is specified of either kind, it is possible that no action will be taken for a candidate change row. WHEN句でAND副句が省略された場合、その句はその種類の最終到達可能句(MATCHEDまたはNOT MATCHED)になります。 その種類の後のWHEN句が指定された場合、到達不能である可能性があり、エラーが発生します。 いずれの種類の最終到達可能句も指定されていない場合、候補変更行に対してアクションが実行されない可能性があります。

The order in which rows are generated from the data source is indeterminate by default. A <replaceable class="parameter">source_query</replaceable> can be used to specify a consistent ordering, if required, which might be needed to avoid deadlocks between concurrent transactions. デフォルトでは、データソースから行が生成される順序は不定です。 source_queryを使用して、必要に応じて一貫した順序を指定できます。 これは、コンカレント・トランザクション間のデッドロックを回避するために必要になる場合があります。

There is no <literal>RETURNING</literal> clause with <command>MERGE</command>. Actions of <command>INSERT</command>, <command>UPDATE</command> and <command>DELETE</command> cannot contain <literal>RETURNING</literal> or <literal>WITH</literal> clauses. MERGEにはRETURNING句はありません。 INSERTUPDATEDELETEのアクションにRETURNING句やWITH句を含めることはできません。

When <command>MERGE</command> is run concurrently with other commands that modify the target table, the usual transaction isolation rules apply; see <xref linkend="transaction-iso"/> for an explanation on the behavior at each isolation level. You may also wish to consider using <command>INSERT ... ON CONFLICT</command> as an alternative statement which offers the ability to run an <command>UPDATE</command> if a concurrent <command>INSERT</command> occurs. There are a variety of differences and restrictions between the two statement types and they are not interchangeable. MERGEをターゲットテーブルを変更する他のコマンドと同時に実行すると、通常のトランザクション分離ルールが適用されます。 各分離レベルでの動作の説明は13.2を参照してください。 また、INSERT ... ON CONFLICTを代替文として使用することも検討できます。 この文は、同時INSERTが発生した場合にUPDATEを実行する機能を提供します。 2つの文タイプの間には様々な違いや制限があり、相互に交換することはできません。

<title>Examples</title>

Perform maintenance on <literal>customer_accounts</literal> based upon new <literal>recent_transactions</literal>. 新規recent_transactionsに基づいて、customer_accountsのメンテナンスを実行します。

MERGE INTO customer_account ca
USING recent_transactions t
ON t.customer_id = ca.customer_id
WHEN MATCHED THEN
  UPDATE SET balance = balance + transaction_value
WHEN NOT MATCHED THEN
  INSERT (customer_id, balance)
  VALUES (t.customer_id, t.transaction_value);

Notice that this would be exactly equivalent to the following statement because the <literal>MATCHED</literal> result does not change during execution. MATCHEDの結果は実行中に変更されないため、これは次の文とまったく同じになります。

MERGE INTO customer_account ca
USING (SELECT customer_id, transaction_value FROM recent_transactions) AS t
ON t.customer_id = ca.customer_id
WHEN MATCHED THEN
  UPDATE SET balance = balance + transaction_value
WHEN NOT MATCHED THEN
  INSERT (customer_id, balance)
  VALUES (t.customer_id, t.transaction_value);

Attempt to insert a new stock item along with the quantity of stock. If the item already exists, instead update the stock count of the existing item. Don't allow entries that have zero stock. 在庫数量とともに新規在庫品目を挿入しようとしました。 品目がすでに存在する場合は、既存品目の在庫数を更新します。 在庫数が0のエントリは許可しません。

MERGE INTO wines w
USING wine_stock_changes s
ON s.winename = w.winename
WHEN NOT MATCHED AND s.stock_delta > 0 THEN
  INSERT VALUES(s.winename, s.stock_delta)
WHEN MATCHED AND w.stock + s.stock_delta > 0 THEN
  UPDATE SET stock = w.stock + s.stock_delta
WHEN MATCHED THEN
  DELETE;

The <literal>wine_stock_changes</literal> table might be, for example, a temporary table recently loaded into the database. wine_stock_changesテーブルは、たとえば、最近データベースにロードされた一時テーブルです。

互換性

<title>Compatibility</title>

This command conforms to the <acronym>SQL</acronym> standard. このコマンドは、SQL標準に準拠しています。

The WITH clause and <literal>DO NOTHING</literal> action are extensions to the <acronym>SQL</acronym> standard. WITH句とDO NOTHINGアクションは、SQL標準の拡張です。