Home > Database > Mysql Tutorial > Oracle中merge用法

Oracle中merge用法

WBOY
Release: 2016-06-07 17:14:36
Original
1186 people have browsed it

MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句。 通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查

MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句。 通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询,连接条件匹配上的进行UPDATE,,无法匹配的执行INSERT。这个语法仅需要一次全表扫描就完成了全部工作,执行效率要高于INSERT+UPDATE。

语法

MERGE [INTO [schema .] table [t_alias]

  USING [schema .] { table | view | subquery } [t_alias]

  ON ( condition )

  WHEN MATCHED THEN merge_update_clause

  WHEN NOT MATCHED THEN merge_insert_clause;

1、UPDATE或INSERT子句是可选的

2、UPDATE和INSERT子句可以加WHERE子句

3、在ON条件中使用常量过滤谓词来insert所有的行到目标表中,不需要连接源表和目标表

4、UPDATE子句后面可以跟DELETE子句来去除一些不需要的行

首先创建示例表:

create table PRODUCTS

      (

      PRODUCT_ID INTEGER,

      PRODUCT_NAME VARCHAR2(60),

      CATEGORY VARCHAR2(60)

      );

 

      insert into PRODUCTS values (1501, 'VIVITAR 35MM', 'ELECTRNCS');

      insert into PRODUCTS values (1502, 'OLYMPUS IS50', 'ELECTRNCS');

      insert into PRODUCTS values (1600, 'PLAY GYM', 'TOYS');

      insert into PRODUCTS values (1601, 'LAMAZE', 'TOYS');

      insert into PRODUCTS values (1666, 'HARRY POTTER', 'DVD');

      commit;

 

      create table NEWPRODUCTS

      (

      PRODUCT_ID INTEGER,

      PRODUCT_NAME VARCHAR2(60),

      CATEGORY VARCHAR2(60)

      );

 

      insert into NEWPRODUCTS values (1502, 'OLYMPUS CAMERA', 'ELECTRNCS');

      insert into NEWPRODUCTS values (1601, 'LAMAZE', 'TOYS');

      insert into NEWPRODUCTS values (1666, 'HARRY POTTER', 'TOYS');

      insert into NEWPRODUCTS values (1700, 'WAIT INTERFACE', 'BOOKS');

      commit;

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template