以下代码在SQL中出现错误
关键字'选择'附近的语法不正确。
这是我的合并代码,其中InstalledSoftwareList是一个已使用的已定义表。
MERGE [dbo].[TableName] AS TargetTable
USING UDTableName AS SourceTable
ON (TargetTable.[EId] = SourceTable.[EId]
AND TargetTable.[MId] = SourceTable.[MId]
AND TargetTable.PackageId = (SELECT Id FROM [PackagesDummyTable] SP WHERE SP.[Version] = SourceTable.[Version] AND SP.[Name] = SourceTable.[Name])
)
WHEN NOT MATCHED BY TARGET -- If the records in the Customer table is not matched?-- then INSERT the record
THEN INSERT ([Guid], [PackageId], [CName], [UUID], [MAC], [Date], [isUninstalled], [LastUpdatedDateTime], [DataCapturedTime], [SGuid], [UniqueId], [MId], [EId])
Select SourceTable.Guid,SP.PackageId,SourceTable.CName,SourceTable.UUID,SourceTable.MAC,SourceTable.Date,SourceTable.isUninstalled,GETUTCDATE(),SourceTable.DataCapturedTime,SourceTable.SGuid, SourceTable.UniqueId, SourceTable.MId, SourceTable.EId
FROM [PackagesDummyTable] SP WHERE SP.[Version] = SourceTable.[Version] AND SP.[Name] = SourceTable.[Name];
我指的是https://msdn.microsoft.com/en-us/library/bb510625.aspx。我的语法似乎是正确的。 谁可以帮我这个事。我正在使用SQL Azure。
答案 0 :(得分:1)
MERGE
操作, CRUD
会很好。在这种情况下,我们只需要插入新记录。你能尝试这样的事吗:
INSERT INTO ([Guid], [PackageId], [CName], [UUID], [MAC], [Date], [isUninstalled], [LastUpdatedDateTime], [DataCapturedTime], [SGuid], [UniqueId], [MId], [EId])
SELECT SourceTable.Guid,SP.PackageId,SourceTable.CName,SourceTable.UUID,SourceTable.MAC,SourceTable.Date,SourceTable.isUninstalled,GETUTCDATE(),SourceTable.DataCapturedTime,SourceTable.SGuid, SourceTable.UniqueId, SourceTable.MId, SourceTable.EId
-- we need these two tables in order to import data
FROM UDTableName AS SourceTable
INNER JOIN [PackagesDummyTable] SP
ON SP.[Version] = SourceTable.[Version]
AND SP.[Name] = SourceTable.[Name]
-- we are joing this table in order to check if there is new data for import
LEFT JOIN [dbo].[TableName] AS TargetTable
ON TargetTable.[EId] = SourceTable.[EId]
AND TargetTable.[MId] = SourceTable.[MId]
-- we are importing only the data that is new
WHERE TargetTable.PackageId IS NULL;
答案 1 :(得分:1)
正如@gotqn所说,如果你只需要访问新数据,你可以直接插入语句。
如果要求您必须使用MERG INTo,您可以将脚本更改为
MERGE [dbo].[TableName] AS TargetTable
USING (
SELECT UN.[EId],UN.[MId],SP.ID ,UN.Guid,SP.PackageId,UN.CName,UN.UUID,UN.MAC,UN.Date,UN.isUninstalled
,UN.DataCapturedTime,UN.SGuid, UN.UniqueId
FROM UDTableName AS UN AS
INNER JOIN [PackagesDummyTable] SP ON SP.[Version] = UN.[Version] AND SP.[Name] = UN.[Name]
) SourceTable
ON TargetTable.[EId] = SourceTable.[EId]
AND TargetTable.[MId] = SourceTable.[MId]
AND TargetTable.PackageId = SourceTable.Id
WHEN NOT MATCHED BY TARGET -- If the records in the Customer table is not matched?-- then INSERT the record
THEN INSERT ([Guid], [PackageId], [CName], [UUID], [MAC], [Date], [isUninstalled], [LastUpdatedDateTime], [DataCapturedTime], [SGuid], [UniqueId], [MId], [EId])
VALUES( SourceTable.Guid,SourceTable.PackageId,SourceTable.CName,SourceTable.UUID,SourceTable.MAC,SourceTable.Date,SourceTable.isUninstalled,GETUTCDATE(),SourceTable.DataCapturedTime,SourceTable.SGuid, SourceTable.UniqueId, SourceTable.MId, SourceTable.EId)
;