sql-server - 在合并查询的 Insert 语句中使用 Select From table

标签 sql-server azure

下面的代码在 SQL 中出现错误

Incorrect syntax near the keyword 'Select'.

这是我的合并代码,其中 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。

最佳答案

正如@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)
    ;

关于sql-server - 在合并查询的 Insert 语句中使用 Select From table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40859146/

相关文章:

sql - 在 T-SQL 中透视数据

sql - 根据列值动态决定连接表

c# - Asp.Net,SQL和TimeZones

azure - 存储客户端异常 : The specified message does not exist?

sql - 有没有更好的方法来过滤每个组中存在的数据?

mysql - 在 sql 中获取错误的日期列差异?

android - 自动检测语音识别语言

azure - 指示在处理时不应缩小 Azure 辅助角色的信号

azure - 如何使用 terraform 创建 Azure 数据工厂 Azure SQL 数据库数据集

c# - OAuth 隐式流的 Azure AD 响应问题