2

我试图在两个临时表中使用“MERGE”,但没有得到结果。

错误:关键字“AS”附近的语法不正确。

    Create Table #tmp1
    (
    [Server] varchar(4),
    [DateTime] datetime,
    IdComponent int,
    AvgTimeTaken int
    )

    Create Table #tmp2
    (
    [Server] varchar(4),
    [DateTime] datetime,
    IdComponent int,
    AvgTimeTaken int
    )

    insert into #tmp1 values ('BE01','2012-08-01 00:00:00',1,100)
    insert into #tmp1 values ('BE02','2012-08-01 00:00:00',2,100)
    insert into #tmp1 values ('BE03','2012-08-01 00:00:00',3,100)
    insert into #tmp1 values ('BE04','2012-08-01 00:00:00',4,100)
    insert into #tmp1 values ('BE05','2012-08-01 00:00:00',5,100)


    insert into #tmp2 values ('BE01','2012-08-01 00:00:00',1,100)
    insert into #tmp2 values ('BE02','2012-08-01 00:00:00',2,200)
    insert into #tmp2 values ('BE03','2012-08-01 00:00:00',3,300)
    insert into #tmp2 values ('BE04','2012-08-01 01:00:00',4,400)
    insert into #tmp2 values ('BE05','2012-08-01 02:00:00',5,500)

    MERGE #tmp1 AS [Target]
    USING #tmp2 AS [Source]
    ON ([Target].[Server] = [Source].[Server] 
        AND [Target].[DateTime] =  [Source].[DateTime] 
        AND [Target].[IdComponent] = [Source].[IdComponent])
    WHEN MATCHED THEN 
        UPDATE
        SET [Target].AvgTimeTaken = [Source].AvgTimeTaken
    WHEN NOT MATCHED THEN   
        INSERT ([Target].[Server], [Target].[DateTime], [Target].IdComponent, [Target].AvgTimeTaken)
VALUES ([Source].[Server], [Source].[DateTime], [Source].IdComponent, [Source].AvgTimeTaken);

我不确定我会不会错。最后还有一个分号。请帮忙!

4

3 回答 3

6

我之前遇到过这个错误,这是因为 SQL Server 2008 R2 不支持合并语法,除非您通过执行以下命令设置兼容模式:

ALTER DATABASE mydatabase SET COMPATIBILITY_LEVEL = 100

其中mydatabase是您要应用它的数据库的名称。

于 2013-05-15T21:13:50.953 回答
2

取出子句Target中的别名。INSERT

Create Table #tmp1 (
    [Server] varchar(4),
    [DateTime] datetime,
    IdComponent int,
    AvgTimeTaken int
);

Create Table #tmp2 (
    [Server] varchar(4),
    [DateTime] datetime,
    IdComponent int,
    AvgTimeTaken int
);

insert into #tmp1 values ('BE01','2012-08-01 00:00:00',1,100);
insert into #tmp1 values ('BE02','2012-08-01 00:00:00',2,100);
insert into #tmp1 values ('BE03','2012-08-01 00:00:00',3,100);
insert into #tmp1 values ('BE04','2012-08-01 00:00:00',4,100);
insert into #tmp1 values ('BE05','2012-08-01 00:00:00',5,100);


insert into #tmp2 values ('BE01','2012-08-01 00:00:00',1,100);
insert into #tmp2 values ('BE02','2012-08-01 00:00:00',2,200);
insert into #tmp2 values ('BE03','2012-08-01 00:00:00',3,300);
insert into #tmp2 values ('BE04','2012-08-01 01:00:00',4,400);
insert into #tmp2 values ('BE05','2012-08-01 02:00:00',5,500);

MERGE #tmp1 AS Target
USING #tmp2 AS Source
ON (Target.Server = Source.Server 
    AND Target.DateTime =  Source.DateTime 
    AND Target.IdComponent = Source.IdComponent)
WHEN MATCHED THEN 
    UPDATE
    SET Target.AvgTimeTaken = Source.AvgTimeTaken
WHEN NOT MATCHED THEN   
    INSERT (Server, DateTime, IdComponent, AvgTimeTaken)
VALUES (Source.Server, Source.DateTime, Source.IdComponent, Source.AvgTimeTaken);
于 2012-08-02T23:46:49.177 回答
0

MERGE 语句中使用的插入列列表不能包含多部分标识符。请改用单部分标识符。

于 2014-10-08T20:47:48.487 回答