我试图在两个临时表中使用“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);
我不确定我会不会错。最后还有一个分号。请帮忙!