SQL Server过程 - 关键字“WHEN”附近的语法不正确

问题描述 投票:0回答:1
USE [DATABASE_NAME]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[upsert_sfdc_account] 
    @sfdc_account_S [dbo].[sfdc_account_type] READONLY
AS
BEGIN
    /* SELECT * FROM [dbo].[sfdc_account] */

    DECLARE @SetColumns VARCHAR(8000) = (SELECT CONCAT(QUOTENAME(COLUMN_NAME),' = S.',QUOTENAME(COLUMN_NAME),',',CHAR(10))
                                         FROM INFORMATION_SCHEMA.COLUMNS 
                                         WHERE TABLE_NAME = 'sfdc_account'  
                                         FOR XML PATH(''))

    DECLARE @Columns VARCHAR(4000) = (SELECT QUOTENAME(COLUMN_NAME) + ','
                                      FROM INFORMATION_SCHEMA.COLUMNS 
                                      WHERE TABLE_NAME = 'sfdc_account' 
                                      FOR XML PATH(''))

  SET @SetColumns = SUBSTRING(@SetColumns, 53, LEN(@SetColumns) - 54)
  SET @Columns = SUBSTRING(@Columns, 6, LEN(@Columns) - 6)

  SELECT @SetColumns
  SELECT @Columns

  MERGE [dbo].[sfdc_account] AS T
  USING @sfdc_account_S AS S
  ON (T.Salesforce_Id = S.Salesforce_Id)
  WHEN MATCHED THEN
    UPDATE SET @SetColumns
  WHEN NOT MATCHED THEN
    INSERT(@Columns)
    VALUES(@Columns)
END

我收到此错误:

消息156,级别15,状态1,过程upsert_sfdc_account,第40行 关键字“WHEN”附近的语法不正确。

我似乎无法正确执行此操作。我错过了什么?

子字符串产生以下值:

@SetColumns

[IsDeleted] = S. [IsDeleted],[MasterRecordId] = S. [MasterRecordId],[Name] = S. [Name],[Type] = S. [Type],[RecordTypeId] = S. [RecordTypeId], [ParentId] = S. [ParentId],[BillingStreet] = S. [BillingStreet],[BillingCity] = S. [BillingCity],[BillingState] = S. [BillingState],[BillingPostalCode] = S. [BillingPostalCode], [BillingCountry] = S. [BillingCountry],[BillingStateCode] = S. [BillingStateCode],[BillingCountryCode] = S. [BillingCountryCode],[BillingLatitude] = S. [BillingLatitude],[BillingLongitude] = S. [BillingLongitude], [BillingGeocodeAccuracy] = S. [BillingGeocodeAccuracy],[ShippingStreet] = S. [ShippingStreet],[ShippingCity] = S. [ShippingCity],[ShippingState] = S. [ShippingState],[ShippingPostalCode] = S. [ShippingPostalCode], [ShippingCountry] = S. [ShippingCountry],[ShippingStateCode] = S. [ShippingStateCode],[ShippingCountryCode] = S. [ShippingCountryCode],[ShippingLatitude] = S. [ShippingLatitude],[ShippingLongitude] = S. [ShippingLongitude], [ShippingGeocodeAccuracy] = S. [Sh ippingGeocodeAccuracy],[Phone] = S. [Phone],[Fax] = S. [Fax],[AccountNumber] = S. [AccountNumber],[Website] = S. [Website],[PhotoUrl] = S. [ PhotoUrl],[Sic] = S. [Sic],[Industry] = S. [Industry],[AnnualRevenue] = S. [AnnualRevenue],[NumberOfEmployees] = S. [NumberOfEmployees],[Ownership] = S. [所有权],[TickerSymbol] = S. [TickerSymbol],[描述] = S. [描述],[评级] = S. [评级],[网站] = S. [网站],[CurrencyIsoCode] = S. [ CurrencyIsoCode],[OwnerId] = S. [OwnerId],[CreatedDate] = S. [CreatedDate],[CreatedById] = S. [CreatedById],[LastModifiedDate] = S. [LastModifiedDate],[LastModifiedById] = S. [ LastModifiedById],[SystemModstamp] = S. [SystemModstamp],[LastActivityDate] = S. [LastActivityDate],[LastViewedDate] = S. [LastViewedDate],[Sales_Ready__c] = S. [Sales_Ready__c]

@列

[Salesforce_Id],[请将isDeleted],[MasterRecordId],[姓名],[类型],[RecordTypeId],[的ParentId],[BillingStreet],[结算城市],[BillingState],[BillingPostalCode],[BillingCountry],[BillingStateCode ],[BillingCountryCode],[BillingLatitude],[BillingLongitude],[BillingGeocodeAccuracy],[ShippingStreet],[ShippingCity],[ShippingState],[ShippingPostalCode],[ShippingCountry],[ShippingStateCode],[ShippingCountryCode],[ShippingLatitude] [ShippingLongitude],[ShippingGeocodeAccuracy],[电话],[传真],[账户号码],[公司网址],[PhotoUrl],[原文],[行业],[AnnualRevenue],[NumberOfEmployees],[所有权],[TickerSymbol ],[描述],[评分],[网站],[CurrencyIsoCode],[OWNERID],[CreatedDate],[CreatedById],[LastModifiedDate],[LastModifiedById],[SystemModstamp],[LastActivityDate],[LastViewedDate] [Sales_Ready__c]

sql azure-sql-database
1个回答
1
投票

您需要一个分号才能结束合并。

https://docs.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql

MERGE语句需要使用分号(;)作为语句终止符。

© www.soinside.com 2019 - 2024. All rights reserved.