SQL Server中的等价语法来自mysql

时间:2015-12-15 08:44:40

标签: mysql sql sql-server

SQL Server中是否有类似于mysql的等效语法

platform :ios, '8.0'

[OR]

REPLACE INTO

实施例。查询INSERT INTO ... ON DUPLICATE KEY UPDATE

MySQL

我正在尝试将其转换为T-SQL

1 个答案:

答案 0 :(得分:1)

您可以使用merge

MERGE supply_data AS t
    USING (SELECT '1','be','dept','loc', ...) AS s (pid, be, val_1, val_2, ...)
    ON (t.pid = s.pid and t.be = s.be)
WHEN MATCHED THEN 
    UPDATE SET clm_1 = val_1, clm_2 = s.val_2, ...
WHEN NOT MATCHED THEN
    INSERT (pid, be, clm_1, clm_2, ...)
    VALUES (pid, be, val_1, val_2, ...)

试试这个;

MERGE supply_data AS t 
    USING (
        SELECT 1, 'be','dept','loc','team', 2016,
        1, 1, 1, 1,
        1, 1, 1, 1,
        1, 1, 1, 1
    ) 
    AS s (
        pid, be, dept, location, team, aopyr,
        jansupply, febsupply, marsupply, aprsupply, 
        maysupply, junsupply, julsupply, augsupply,
        sepsupply, octsupply, novsupply, decsupply
    )
    ON (
        t.pid = s.pid and t.be = s.be and 
        t.location=s.location and t.team=s.team and 
        t.aopyr=s.aopyr
    ) 
WHEN MATCHED THEN
    UPDATE SET 
        jansupply = s.jansupply, febsupply = s.febsupply, 
        marsupply = s.marsupply, aprsupply = s.aprsupply,  
        maysupply = s.maysupply, junsupply = s.junsupply, 
        julsupply = s.julsupply, augsupply = s.augsupply, 
        sepsupply = s.sepsupply, octsupply = s.octsupply, 
        novsupply = s.novsupply, decsupply = s.decsupply  
WHEN NOT MATCHED THEN 
    INSERT (
        pid, be, dept, location, team, aopyr,
        jansupply, febsupply, marsupply, aprsupply, 
        maysupply, junsupply, julsupply, augsupply,
        sepsupply, octsupply, novsupply, decsupply
    ) 
    VALUES (
        s.pid, s.be, s.dept, s.location, s.team, s.aopyr,
        s.jansupply, s.febsupply, s.marsupply, s.aprsupply, 
        s.maysupply, s.junsupply, s.julsupply, s.augsupply,
        s.sepsupply, s.octsupply, s.novsupply, s. decsupply
    )