在MERGE INTO语句中使用ON的语法问题

时间:2017-08-10 17:07:10

标签: sql-server merge

智能感知错误:

  

Msg 156,Level 15,State 1,Procedure Location_CVT_Insert,Line 19 [Batch Start line 2]
  关键字' ON'。

附近的语法不正确

存储过程代码:

DROP PROCEDURE IF EXISTS [dbo].[Location_CVT_Insert]
GO

CREATE PROCEDURE [dbo].[Location_CVT_Insert]
     (@Location_NotificationJson NVARCHAR(MAX))
AS 
BEGIN
    MERGE INTO Location_CVT AS C
    USING (SELECT 
               deviceID, lastSeen, locationMapHierarchy, locationCoordinateX, 
               locationCoordinateY, locationCoordinateUnit, geoCoordinateLat,
               geoCoordinateLong, geoCoordinateUnit
           FROM 
               OPENJSON(@Location_NotificationJson)
           WITH 
               (deviceId nchar(17),
                lastSeen varchar(128),
                locationMapHierarchy nvarchar(256),
                locationCoordinateX float,
                locationCoordinateY float,
                locationCoordinateUnit nvarchar(64),
                geoCoordinateLat float,
                geoCoordinateLong float,
                geoCoordinateUnit nvarchar(64)) AS InputJSON) ON (C.deviceId = InputJSON.deviceId)

    WHEN MATCHED THEN
        UPDATE 
            SET C.deviceId = InputJSON.deviceId,
                C.lastSeen = InputJSON.lastSeen,
                C.locationMapHierarchy = InputJSON.locationMapHierarchy,
                C.locationCoordinateX = InputJSON.locationCoordinateX,
                C.locationCoordinateY = InputJSON.locationCoordinateY,
                C.locationCoordinateUnit = InputJSON.locationCoordinateUnit,
                C.geoCoordinateLat = InputJSON.geoCoordinateLat,
                C.geoCoordinateLong = InputJSON.geoCoordinateLong,
                C.geoCoordinateUnit = InputJSON.geoCoordinateUnit

    WHEN NOT MATCHED THEN
        INSERT (deviceId, lastSeen, locationMapHierarchy, 
                locationCoordinateX, locationCoordinateY, 
                locationCoordinateUnit, geoCoordinateLat, 
                geoCoordinateLong, geoCoordinateUnit)
        VALUES (InputJSON.deviceId, InputJSON.lastSeen, InputJSON.locationMapHierarchy, 
                InputJSON.locationCoordinateX, InputJSON.locationCoordinateY, 
                InputJSON.locationCoordinateUnit, InputJSON.geoCoordinateLat, 
                InputJSON.geoCoordinateLong, InputJSON.geoCoordinateUnit);
END

似乎ON语句是问题所在。我想从JSON对象(SQL Server 2016)中进行选择,如果我的Location_CVT表中有关于设备ID的匹配,请更新此条目,否则插入新记录。

我正在尝试做类似以下示例的事情。

MERGE INTO Person AS P
USING (
    SELECT *
    FROM  OPENJSON(@json)
          WITH (id int, firstName nvarchar(50), lastName nvarchar(50),
                age int, dateOfBirth datetime2) InputJSON
    ON (P.id = InputJSON.id)
WHEN MATCHED THEN
    UPDATE SET P.firstName = InputJSON.firstName,
               P.lastName = InputJSON.lastName,
               P.age = InputJSON.age,
               P.dateOfBirth = InputJSON.dateOfBirth
WHEN NOT MATCHED THEN
    INSERT (firstName, lastName, age, dateOfBirth)
    VALUES (InputJSON.firstName, InputJSON.lastName, InputJSON.age, 
            InputJSON.dateOfBirth);

来源:https://www.codeproject.com/Articles/1087995/Inserting-JSON-Text-into-SQL-Server-Table

1 个答案:

答案 0 :(得分:2)

如果你的括号跨越多行,你确保你的开括号和闭括号有一致的缩进,问题就会变得更清楚了(至少对我来说是这样)。

public getIndex(boo booObject)
{
   int index = fooList.IndexOf(booObject);
}

正如您现在可以看到的那样,您用于源的子查询没有别名。它应该是:

MERGE INTO Location_CVT AS C
USING 
(   SELECT 
        deviceID, lastSeen, locationMapHierarchy, locationCoordinateX, 
        locationCoordinateY, locationCoordinateUnit, geoCoordinateLat,
        geoCoordinateLong, geoCoordinateUnit
    FROM 
        OPENJSON(@Location_NotificationJson)
    WITH 
        (   deviceId nchar(17),
            lastSeen varchar(128),
            locationMapHierarchy nvarchar(256),
            locationCoordinateX float,
            locationCoordinateY float,
            locationCoordinateUnit nvarchar(64),
            geoCoordinateLat float,
            geoCoordinateLong float,
            geoCoordinateUnit nvarchar(64)
        ) AS InputJSON
)   ------------------------------------------------- ISSUE IS HERE
    ON (C.deviceId = InputJSON.deviceId)        
WHEN MATCHED THEN
    UPDATE 
        SET C.deviceId = InputJSON.deviceId,
            C.lastSeen = InputJSON.lastSeen,
            C.locationMapHierarchy = InputJSON.locationMapHierarchy,
            C.locationCoordinateX = InputJSON.locationCoordinateX,
            C.locationCoordinateY = InputJSON.locationCoordinateY,
            C.locationCoordinateUnit = InputJSON.locationCoordinateUnit,
            C.geoCoordinateLat = InputJSON.geoCoordinateLat,
            C.geoCoordinateLong = InputJSON.geoCoordinateLong,
            C.geoCoordinateUnit = InputJSON.geoCoordinateUnit