zoukankan      html  css  js  c++  java
  • sqlserver 基于一张表更新另外一张表

    SQL Server中如何基于一个表的数据更新另一个表的对应数据?实现的SQL语句有很多方式:

    方式一 INNER JOIN

    UPDATE
        Table_A
    SET
        Table_A.col1 = Table_B.col1,
        Table_A.col2 = Table_B.col2
    FROM
        Some_Table AS Table_A
        INNER JOIN Other_Table AS Table_B
            ON Table_A.id = Table_B.id
    WHERE
        Table_A.col3 = 'cool'
    

    方式二 使用MERGE关键字

    MERGE INTO YourTable T
       USING other_table S 
          ON T.id = S.id
             AND S.tsql = 'cool'
    WHEN MATCHED THEN
       UPDATE 
          SET col1 = S.col1, 
              col2 = S.col2;
    

    或者

    MERGE INTO YourTable T
       USING (
              SELECT id, col1, col2 
                FROM other_table 
               WHERE tsql = 'cool'
             ) S
          ON T.id = S.id
    WHEN MATCHED THEN
       UPDATE 
          SET col1 = S.col1, 
              col2 = S.col2;
    

    注: SQL Server 2008 或者以上版本才支持MERGE关键字

    方式三 使用CTE

    ;WITH CTE
         AS (SELECT T1.Col1,
                    T2.Col1 AS _Col1,
                    T1.Col2,
                    T2.Col2 AS _Col2
             FROM   T1
                    JOIN T2
                      ON T1.id = T2.id
             /*Where clause added to exclude rows that are the same in both tables
               Handles NULL values correctly*/
             WHERE EXISTS(SELECT T1.Col1,
                                 T1.Col2
                           EXCEPT
                           SELECT T2.Col1,
                                  T2.Col2))
    UPDATE CTE
    SET    Col1 = _Col1,
           Col2 = _Col2
    

    方式四 使用EXISTS

    UPDATE suppliers
    SET supplier_name = (SELECT customers.name
                         FROM customers
                         WHERE customers.customer_id = suppliers.supplier_id)
    WHERE EXISTS (SELECT customers.name
                  FROM customers
                  WHERE customers.customer_id = suppliers.supplier_id);


    此外介绍下  SQL Server 2008 Merge into用法

    只有 sqlserver 2008以上版本才有meger关键字

    --创建测试表
    CREATE TABLE t_A
    (MCLASS VARCHAR(20),SNO VARCHAR(20))
    INSERT INTO t_A
    SELECT 'L-1','123'
    UNION ALL
    SELECT 'L-2','234'
    UNION ALL
    SELECT 'L-3','345'

    CREATE TABLE t_B
    (MCLASS VARCHAR(20),SNO VARCHAR(20))
    INSERT INTO t_B
    SELECT 'L-1','999'
    UNION ALL
    SELECT 'L-2','999'
    UNION ALL
    SELECT 'L-4','888'

    --确定目标表
    merge into t_A as a
    --从t_B表中查找MCLASS相同的数据
    using t_B as b on a.MCLASS=b.MCLASS
    --当MCLASS相同时,则更新t_A中SNO字段
    when Matched then update set a.SNO=b.SNO
    --当t_A表中不存在MCLASS相同的数据时,则从t_B表插入目标表t_A
    when Not Matched then Insert(MCLASS,SNO) values(b.MCLASS,b.SNO);

    select * from t_A
    select * from t_B

    drop table t_A
    drop table t_B
    t_a

    MCLASS               SNO

    -------------------- --------------------
    L-1                  123
    L-2                  234
    L-3                  345

    (3 行受影响)
    t_b
    MCLASS               SNO
    -------------------- --------------------
    L-1                  999
    L-2                  999
    L-4                  888

    (3 行受影响)

    ----------------------------------------------------------------------

    更新后的t_a

    MCLASS               SNO
    -------------------- --------------------
    L-1                  999
    L-2                  999
    L-3                  345
    L-4                  888

    (4 行受影响)

    Merge语句可以将一个表中的数据合并到另一个表中,在合并的同时可以进行插入、删除、更新等操作。

    原文链接:https://blog.csdn.net/yole_grise/article/details/15337973

     
  • 相关阅读:
    Linux 守护进程创建
    Linux 进程
    静态库与动态库的制作
    目录文件的操作函数 mkdir ,opendir,readdir,closedir
    获取文件或目录的属性 stat 函数
    文件IO 例子
    文件 IO
    标准 IO fread 与 fwrite 的使用(可以实现二进制流的读写)
    bzoj 2716: [Violet 3]天使玩偶
    cf1175 DE
  • 原文地址:https://www.cnblogs.com/kelly1314/p/11351402.html
Copyright © 2011-2022 走看看