`

Merge Sql

阅读更多

我们经常遇到的一个需求是,先判断数据是否存在,如果存在则更新,否则就插入,以前比较土是用java自己做了。这个在多线程或者多机的情况下就会有些问题,有时候还得让db报个唯一性约束才行。

 

最近和一个同事(以前是oracle的)做项目,发现他写了个牛逼的sql(或者说自己见识太短浅了),特此膜拜下

 

类似这样的

 

MERGE INTO test1 a
  USING all_objects b
    ON (a.object_id = b.object_id)
  WHEN MATCHED THEN
    UPDATE SET a.status = b.status
    WHERE  b.status != 'VALID'
  WHEN NOT MATCHED THEN
    INSERT (object_id, status)
    VALUES (b.object_id, b.status)
    WHERE  b.status != 'VALID';
 

查了些资料发现这种merge sql早就被支持了,只是自己还不知道而已。

 

<--------------------------------------不华丽的分界线--------------------------------------------------------------------->

 

例如wiki上的  http://en.wikipedia.org/wiki/Merge_%28SQL%29

 

Merge (SQL)

From Wikipedia, the free encyclopedia
Jump to: navigation , search

A relational database management system uses SQL MERGE (upsert ) statements to INSERT new records or UPDATE existing records depending on whether or not a condition matches. It was officially introduced in the SQL:2008 standard.

Contents

[hide ]

  Usage

MERGE INTO table_name

 USING table_reference

 ON (condition

)
  WHEN MATCHED THEN
  UPDATE SET column1

 = value1

 [, column2

 = value2

 ...]
  WHEN NOT MATCHED THEN
  INSERT (column1

 [, column2

 ...]) VALUES (value1

 [, value2 ...


Other non-standard implementations

Some other database management systems support this, or very similar behavior, through their own, non-standard SQL extensions.

MySQL , for example, supports the use of INSERT ... ON DUPLICATE KEY UPDATE syntax[ 1] which can be used to achieve the same effect. It also supports REPLACE INTO syntax[ 2] , which first deletes the row, if exists, and then inserts the new one.

SQLite 's INSERT OR REPLACE INTO works similarly.

Firebird supports both MERGE INTO and a single-row version, UPDATE OR INSERT INTO tablename (columns) VALUES (values) [MATCHING (columns)] , but the latter does not give you the option to take different actions on insert vs. update (e.g. setting a new sequence value only for new rows, not for existing ones.)

  References

  1. ^ MySQL :: MySQL 5.1 Reference Manual :: 12.2.4.3 INSERT ... ON DUPLICATE KEY UPDATE Syntax
  2. ^ MySQL 5.1 Reference Manual: 11.2.6 REPLACE Syntax

  External links

 

<---------------------------------------------不华丽的分界线--------------------------------------------------------------->

同时也找了

http://www.oracle-base.com/articles/10g/MergeEnhancements10g.php

 

MERGE Statement Enhancements in Oracle Database 10g

Oracle 10g includes a number of amendments to the MERGE statement making it more flexible.

Test Table

The following examples use the table defined below.

CREATE TABLE test1 AS
SELECT *
FROM   all_objects
WHERE  1=2;

Optional Clauses

The MATCHED and NOT MATCHED clauses are now optional making all of the following examples valid.

-- Both clauses present.
MERGE INTO test1 a
  USING all_objects b
    ON (a.object_id = b.object_id)
  WHEN MATCHED THEN
    UPDATE SET a.status = b.status
  WHEN NOT MATCHED THEN
    INSERT (object_id, status)
    VALUES (b.object_id, b.status);

-- No matched clause, insert only.
MERGE INTO test1 a
  USING all_objects b
    ON (a.object_id = b.object_id)
  WHEN NOT MATCHED THEN
    INSERT (object_id, status)
    VALUES (b.object_id, b.status);

-- No not-matched clause, update only.
MERGE INTO test1 a
  USING all_objects b
    ON (a.object_id = b.object_id)
  WHEN MATCHED THEN
    UPDATE SET a.status = b.status;

Conditional Operations

Conditional inserts and updates are now possible by using a WHERE clause on these statements.

-- Both clauses present.
MERGE INTO test1 a
  USING all_objects b
    ON (a.object_id = b.object_id)
  WHEN MATCHED THEN
    UPDATE SET a.status = b.status
    WHERE  b.status != 'VALID'
  WHEN NOT MATCHED THEN
    INSERT (object_id, status)
    VALUES (b.object_id, b.status)
    WHERE  b.status != 'VALID';

-- No matched clause, insert only.
MERGE INTO test1 a
  USING all_objects b
    ON (a.object_id = b.object_id)
  WHEN NOT MATCHED THEN
    INSERT (object_id, status)
    VALUES (b.object_id, b.status)
    WHERE  b.status != 'VALID';

-- No not-matched clause, update only.
MERGE INTO test1 a
  USING all_objects b
    ON (a.object_id = b.object_id)
  WHEN MATCHED THEN
    UPDATE SET a.status = b.status
    WHERE  b.status != 'VALID';

DELETE Clause

An optional DELETE WHERE clause can be used to clean up after a merge operation. Only those rows which match both the ON clause and the DELETE WHERE clause are deleted.

MERGE INTO test1 a
  USING all_objects b
    ON (a.object_id = b.object_id)
  WHEN MATCHED THEN
    UPDATE SET a.status = b.status
    WHERE  b.status != 'VALID'
  DELETE WHERE (b.status = 'VALID');

For further information see:

分享到:
评论
1 楼 lionbule 2010-10-30  
*****

相关推荐

    SQL中Merge用法详解

    MERGE语句是SQL语句的一种。在SQL Server、Oracle数据库中可用,MySQL、PostgreSQL中不可用。MERGE是Oracle9i新增的语法,用来合并UPDATE和INSERT语句。通过MERGE语句,根据一张表(原数据表,source table)或子...

    Merge-Sql.zip

    MERGE语句在SQL中扮演着关键角色,它提供了一种高效且灵活的方式来合并两个数据集,无论是更新、插入还是删除,都能一气呵成。本文将深入探讨MERGE语句的使用,以及在不支持MERGE的数据库环境中可能采取的替代策略。...

    使用BULK COLLECT, MERGE 语句提高sql执行效率

    详细介绍了使用 BULK COLLECT 进行批量操作 提高sql的执行效率 使用MERGE INTO USING 一条sql搞定 新增和修改 使用connect by 进行递归树查询

    SQLServer中merge函数用法详解

    `Merge`函数是SQL Server 2008引入的一个强大的数据操纵语言(DML)关键字,它可以合并`Insert`、`Update`和`Delete`操作到一个单一的语句中,极大地提高了数据库维护的效率和代码的简洁性。在本文中,我们将深入...

    SQL server 触发器,在触发Merge过程中,逐行触发的解决办法 用group by 避免是一次触发中的多行更新或删除。

    Merge语句是一种复杂的SQL操作,它结合了INSERT、UPDATE和DELETE,用于同步两个数据源。当Merge触发器被定义为在这些操作上触发时,如果处理不当,可能会导致一次性处理大量行,而非逐行处理。这可能导致资源消耗过...

    merge_sql_files.bat

    merge_sql_files

    sql学习 merge误区探讨.sql

    sql学习 merge误区探讨.sql

    sql学习 merge经典案例.sql

    sql学习 merge经典案例.sql

    Mybatis批量foreach merge into的用法

    Mybatis批量foreach merge into的用法是通过Mybatis的动态SQL语法foreach循环插入实现的,这种方法可以批量插入时间价格表数据。如果某个套餐的某天的价格存在,则更新,不存在则插入。下面是该方法的详细介绍: ...

    SQLServer2008新特性(QuickView)

    如数据变化跟踪、同步编程模型、冲突检测、FILESTREAM数据类型、集成全文本搜索、稀疏列、巨大...MERGE SQL语句、数据概况、星形连接优化、企业报表引擎、互联网报表部署、锁定计算、BI平台管理和导出到Word和Excel等...

    sql学习 merge有啥用途.sql

    sql学习 merge有啥用途.sql

    sql学习 merge灵活之处.sql

    sql学习 merge灵活之处.sql

    statfilter usage

    上述配置中,`druid.stat.filter.enabled`开启StatFilter,`druid.stat.mergeSql`表示是否合并相同的SQL,`druid.stat.slowSqlMillis`定义了慢SQL的阈值,超过这个时间的SQL会被记录下来。 此外,StatFilter还提供...

    0709-动态SQL-merge-into.sql

    0709-动态SQL-merge-into.sql

    druid的jar包及配置.rar

    &lt;property name="mergeSql" value="true"/&gt; ``` Druid的另一个亮点是其SQL解析能力。通过Druid的Parser组件,可以将SQL语句解析成抽象语法树(AST),便于进行SQL格式化、校验和优化。这对于保持代码整洁、避免...

    利用 sqluldr2导出数据 使用sqlldr导入数据 通过merge into 合并更新数据

    Oracle数据库提供了一套强大的工具,包括SQL*Loader(简称SQLLDR)和SQL Ultra Data Recorder 2(SQLULDR2),来帮助我们处理这些任务。下面将详细阐述如何使用这两个工具以及如何通过`MERGE INTO`语句进行数据合并...

    oracle Merge 函数.doc

    "Oracle Merge 函数详解" Oracle Merge 函数是一种强大的数据操作语句,用于从一个表中选择一些数据更新或者插入到另一个表中。Merge 函数的使用方式有三种:只更新不插入、只插入不更新和既插入也更新。 Merge ...

    Druid_jar包及配置文件.zip

    - `filters`:全局过滤器,可配置如`mergeSql`(合并SQL)等功能。 - `validationQuery`:用于验证连接是否有效的SQL语句。 - `poolPreparedStatements`:是否缓存PreparedStatement对象。 - `...

Global site tag (gtag.js) - Google Analytics