简体   繁体   English

如何在没有 MERGE 语句的情况下执行更新其他插入操作赢得 INFORMIX

[英]How to perform Update else Insert Operation win INFORMIX without MERGE Statement

Let's Suppose I have two tables - Source and Target.假设我有两个表 - Source 和 Target。 I am trying to Load Target table from Source, and the record should be inserted only if it is not present in the target table, else it should be updated.我正在尝试从源加载目标表,只有在目标表中不存在记录时才应插入记录,否则应更新记录。 All the columns should be considered for the comparisons.应考虑所有列进行比较。 Is there any options available in Informix other than Merge statement.除了 Merge 语句之外,Informix 中是否还有其他可用选项。

As you now know , the MERGE statement was not present in Informix 10.00 (or any version prior to 11.50.xC6).正如您现在所知,在 Informix 10.00(或 11.50.xC6 之前的任何版本)中不存在 MERGE 语句。

There isn't a simple way around it.没有简单的方法可以解决。

In outline, the closest approximation is:概括地说,最接近的近似值是:

  1. Identify the primary key columns in the source and target tables — I'm going to assume they're single-column keys with names src_primary_key and tgt_primary_key .确定源表和目标表中的主键列——我将假设它们是名称为src_primary_keytgt_primary_key键。 Life is more complex if they're multi-column keys, but not insuperably so.如果它们是多列键,生活会更加复杂,但并非不可克服。

  2. Nominally, you would insert the missing records using:名义上,您可以使用以下方法插入丢失的记录:

     INSERT INTO Target SELECT * FROM Source WHERE src_primary_key NOT IN (SELECT tgt_primary_key FROM Target)
  3. However, you probably run foul of restrictions on selecting from the table you're also inserting into, so you end up doing:但是,您可能会在从要插入的表中进行选择时遇到限制,因此您最终会这样做:

     SELECT src_primary_key FROM Source WHERE src_primary_key NOT IN (SELECT tgt_primary_key FROM Target) INTO TEMP Missing_Keys INSERT INTO Target SELECT * FROM Source WHERE src_primary_key IN (SELECT src_primary_key FROM Missing_Keys)
  4. Since you want the updates to replace the existing data, you then arrange to create a list of present keys:由于您希望更新替换现有数据,因此您可以安排创建现有密钥列表:

     SELECT src_primary_key FROM Source WHERE src_primary_key IN (SELECT tgt_primary_key FROM Target) INTO TEMP Present_Keys; DELETE FROM Target WHERE tgt_primary_key IN (SELECT src_primary_key FROM Present_Keys) INSERT INTO Target SELECT * FROM Source WHERE src_primary_key IN (SELECT src_primary_key FROM Present_Keys)
  5. All of this has to be done inside a transaction to be safe, of course — probably at REPEATABLE READ isolation for maximum safety.当然,所有这些都必须在事务内完成才能安全——可能在可重复读隔离中实现最大安全性。

There are probably other ways to do this, but this loosely simulates the steps that the MERGE statement would go through.可能还有其他方法可以做到这一点,但这大致模拟了 MERGE 语句将经历的步骤。 You might need to deal with 'Present Keys' before dealing with 'Missing Keys' (so execute step 3 after executing step 4).在处理“丢失密钥”之前,您可能需要处理“当前密钥”(因此在执行步骤 4 后执行步骤 3)。 You might also think about whether to simply delete all the rows from Target that match a row in Source, and then simply insert the contents of Source into Target:您可能还会考虑是否简单地从 Target 中删除与 Source 中的一行匹配的所有行,然后简单地将 Source 的内容插入到 Target 中:

BEGIN WORK;
DELETE FROM Target
 WHERE tgt_primary_key IN (SELECT src_primary_key FROM Source);
INSERT INTO Target SELECT * FROM Source;
COMMIT WORK;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM