0

我想根据以下代码在数据库中插入一行

if table t has doesnot not have row with key X:
      insert into t mystuff..
else
    if  update t set mystuff... where mykey=X if existingversion < NewVersion

我知道正常的合并可以如下使用

MERGE INTO (SELECT * FROM mytable WHERE status='active') old
     USING (SELECT * FROM newtable) new
        ON (new.id = old.id)
      WHEN MATCHED THEN UPDATE SET old.data1=new.data1;

但是我如何处理我的合并条件更新?

4

1 回答 1

0

您可以过滤要更新插入的行,例如:

MERGE INTO (SELECT * FROM mytable WHERE status='active') old
     USING (SELECT * 
              FROM newtable n
             WHERE NOT EXISTS (SELECT NULL 
                                 FROM mytable m
                                WHERE m.id = n.id
                                  AND m.version >= n.version) new
        ON (new.id = old.id)
      WHEN MATCHED THEN UPDATE SET old.data1=new.data1;
于 2013-10-25T15:34:13.960 回答