MERGE INTO

语法:

MERGE INTO table_name alias1
USING (table|view|sub_query) alias2
ON (join condition)
WHEN MATCHED THEN
  UPDATE table_name
   SET col1 = col_val1,
     col2 = col2_val
WHEN NOT MATCHED THEN
  INSERT (column_list) VALUES (column_values);

  

理解:

在alias2中Select出来的数据,每一条都跟alias1进行 ON (join condition)的比较,如果匹配,就进行更新的操作(Update),如果不匹配,就进行插入操作(Insert)。
因此,严格意义上讲,"在一个同时存在Insert和Update语法的Merge语句中,总共Insert/Update的记录数,就是Using语句中alias2的记录数。"

Cross/Outer Apply

语法:

<左表>  {cross|outer} apply <右表>

如:

select *
from Customers as C
cross apply
(select top 2 *
from Orders as O
where C.customerid=O.customerid
order by orderid desc) as CA

理解:

  它是先得出<左表>【Customers】数据,然后将<左表>里的每一条记录,对应条件查询得到<右表>的n条记录,然后进行合并得到n行记录,最后将所有数据行返回。

cross和outer的区别:如果对于左表的某一行记录,右表没有数据,那么cross apply结果中将没有这一行记录,而outer apply结果则会有一条右表都为null的记录。

(实际过程数据库另有优化)

SQL语句 - MERGE INTO 、Cross/Outer Apply用法理解的更多相关文章

  1. sql server cross/outer apply 用法

    这是 sql server 帮助文档关于apply的描述: 使用 APPLY 运算符(2005或以上版本)可以为实现查询操作的外部表表达式返回的每个行调用表值函数.表值函数作为右输入,外部表表达式作为 ...

  2. SQL Server Cross/Outer Apply

    SQL Server2005引入了APPLY运算符,它非常像连接子句,它允许两个表达式直接进行连接,即将左/外部表达式和右/内部表达式连接起来. CROSS APPLY(类比inner join)和O ...

  3. SQL中笛卡尔积-cross join的用法

    在数学中,笛卡尔乘积是指两个集合X和Y的笛卡尓积(Cartesian product),又称直积,表示为X × Y,第一个对象是X的成员而第二个对象是Y的所有可能有序对的其中一个成员 假设集合A={a ...

  4. Sql语句中IN等方面的用法

    select * from txt1 select * from txt2 select * from txt1 where name in (select name from txt2 where ...

  5. sql语句查询结果合并union all用法

    整理别人的sql 大概的思想是用union 和union all --合并重复行select * from Aunion select * from B --不合并重复行select * from A ...

  6. SQL 语句 merge into

    MERGE INTO tb_st_shxxcount tt USING ( SELECT DISTINCT sd.CODE, COUNT (ts.LRDW) count1, TO_CHAR (ts.L ...

  7. sql语句查询结果合并union all用法_数据库技巧

    --合并重复行 select * from A union select * from B --不合并重复行 select * from A union all select * from B 按某个 ...

  8. SQL语句中exists/not exists的用法分析

    作者:Dsw 比如在Northwind数据库中有一个查询为 SELECT c.CustomerId,CompanyName FROM Customers c WHERE EXISTS( SELECT ...

  9. sql语句:CASE WHEN END 的用法

    select b,c, CASE a ' ' ' end from test1

随机推荐

  1. IEnumerable和IQueryable和Linq的查询

    IEnumerable和IEnumerable 1.IEnumerable查询必须在本地执行.并且执行查询前我们必须把所有的数据加载到本地.而且更多的时候.加载的数据有大量的数据是我们不需要的无效数据 ...

  2. VR技术的探索阶段

    转载请声明转载地址:http://www.cnblogs.com/Rodolfo/,违者必究. 早在1929年,在长期使用教练机训练器(机翼变短,不能产生离开地面所需的足够提升力)进行飞行训练之后,E ...

  3. CozyRSS开发记录5-订阅列表栏里的项

    CozyRSS开发记录5-订阅列表栏里的项 1.订阅列表栏里的项的原型图 这里列表项依然参考傲游的RSS阅读器,以后可能会微调. 2.使用ControlTemplate来定制ListBoxItem 给 ...

  4. ubuntu下配置jdk

    1.首先下载jdk-7u51-linux-i586.tar.gz.并将它放在例如/home目录. 2.解压安装 sudo tar zxvf ./jdk-7u51-linux-i586.tar.gz  ...

  5. java线程池ThreadPoolExecutor使用简介

    一.简介线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为:ThreadPoolExecutor(int corePoolSize, int m ...

  6. containing block

    1(position:static和relative) 它的包含块由它最近的块级.单元格(table cell)或者行内块(inline-block)祖先元素创建. 2.position:fixed ...

  7. 【转】HashMap、TreeMap、Hashtable、HashSet和ConcurrentHashMap区别

    转自:http://blog.csdn.net/paincupid/article/details/47746341 一.HashMap和TreeMap区别 1.HashMap是基于散列表实现的,时间 ...

  8. Leetcode 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  9. ACM:SCU 4437 Carries - 水题

    SCU 4437  Carries Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu  Practice  ...

  10. centos使用yum安装软件的时候出现了undefined symbol: CRYPTO_set_locking_callback

    1.问题 在CentOS下使用yum安装软件,结果出现了下面的错误提示: # yum installThere was a problem importing one of the Python mo ...