T-SQL语法错误不存在(或者,至少,我看不到它,并且智能感知)但仍然通知

时间:2013-01-19 10:46:18

标签: sql sql-server tsql sql-server-2008-r2

我有一个克隆数据库模式(表格)的脚本,并添加了一些列来跟踪数据变化(我们经常从遗留ERP中接收数据),这个脚本创建了一些存储过程来检查数据中的变化。数据,它总是像一个魅力,但今天在测试数据库上启动后,它给了我一个错误:

  

消息102,级别15,状态1,过程COMPUTE_AGENT,第9行
  '。'

附近的语法不正确

这是程序:

CREATE PROCEDURE [dbo].[COMPUTE_AGENT] (@Debug INT = 0)
AS
BEGIN
    DECLARE @Actions TABLE (IUD VARCHAR(100))

    INSERT INTO @Actions (IUD)
    SELECT IUD
    FROM (
        MERGE [frontier].[AGENT] AS [Destination]
        USING [dbo].[AGENT] AS [Source]
            ON [Destination].[AgentCode] = [Source].[AgentCode]
        WHEN NOT MATCHED BY TARGET
            THEN
                INSERT (
                    [AgentCode]
                    ,[Name]
                    ,[PersonalTaxCode]
                    ,[VatNumber]
                    ,[Commission]
                    ,[Address]
                    ,[City]
                    ,[ZipCode]
                    ,[Province]
                    ,[Region]
                    ,[Country]
                    ,[Phone]
                    ,[CellPhone]
                    ,[FAX]
                    ,[EMail]
                    ,[Web]
                    ,[LastEditDate]
                    ,[CHECKSUM]
                    ,[SyncroLED]
                    ,[SyncroPS]
                    )
                VALUES (
                    [Source].[AgentCode]
                    ,[Source].[Name]
                    ,[Source].[PersonalTaxCode]
                    ,[Source].[VatNumber]
                    ,[Source].[Commission]
                    ,[Source].[Address]
                    ,[Source].[City]
                    ,[Source].[ZipCode]
                    ,[Source].[Province]
                    ,[Source].[Region]
                    ,[Source].[Country]
                    ,[Source].[Phone]
                    ,[Source].[CellPhone]
                    ,[Source].[FAX]
                    ,[Source].[EMail]
                    ,[Source].[Web]
                    ,[Source].[LastEditDate]
                    ,BINARY_CHECKSUM([Source].[AgentCode], [Source].[Name], [Source].[PersonalTaxCode], [Source].[VatNumber], [Source].[Commission], [Source].[Address], [Source].[City], [Source].[ZipCode], [Source].[Province], [Source].[Region], [Source].[Country], [Source].[Phone], [Source].[CellPhone], [Source].[FAX], [Source].[EMail], [Source].[Web], [Source].[LastEditDate])
                    ,GETDATE()
                    ,0
                    )
        WHEN MATCHED
            AND [Destination].[CHECKSUM] <> BINARY_CHECKSUM([Source].[AgentCode], [Source].[Name], [Source].[PersonalTaxCode], [Source].[VatNumber], [Source].[Commission], [Source].[Address], [Source].[City], [Source].[ZipCode], [Source].[Province], [Source].[Region], [Source].[Country], [Source].[Phone], [Source].[CellPhone], [Source].[FAX], [Source].[EMail], [Source].[Web], [Source].[LastEditDate])
            THEN
                UPDATE
                SET [Destination].[AgentCode] = [Source].[AgentCode]
                    ,[Destination].[Name] = [Source].[Name]
                    ,[Destination].[PersonalTaxCode] = [Source].[PersonalTaxCode]
                    ,[Destination].[VatNumber] = [Source].[VatNumber]
                    ,[Destination].[Commission] = [Source].[Commission]
                    ,[Destination].[Address] = [Source].[Address]
                    ,[Destination].[City] = [Source].[City]
                    ,[Destination].[ZipCode] = [Source].[ZipCode]
                    ,[Destination].[Province] = [Source].[Province]
                    ,[Destination].[Region] = [Source].[Region]
                    ,[Destination].[Country] = [Source].[Country]
                    ,[Destination].[Phone] = [Source].[Phone]
                    ,[Destination].[CellPhone] = [Source].[CellPhone]
                    ,[Destination].[FAX] = [Source].[FAX]
                    ,[Destination].[EMail] = [Source].[EMail]
                    ,[Destination].[Web] = [Source].[Web]
                    ,[Destination].[LastEditDate] = [Source].[LastEditDate]
                    ,[Destination].[CHECKSUM] = BINARY_CHECKSUM([Source].[AgentCode], [Source].[Name], [Source].[PersonalTaxCode], [Source].[VatNumber], [Source].[Commission], [Source].[Address], [Source].[City], [Source].[ZipCode], [Source].[Province], [Source].[Region], [Source].[Country], [Source].[Phone], [Source].[CellPhone], [Source].[FAX], [Source].[EMail], [Source].[Web], [Source].[LastEditDate])
                    ,[Destination].[SyncroLED] = GETDATE()
                    ,[Destination].[SyncroPS] = 0
        WHEN NOT MATCHED BY SOURCE
            AND [Destination].[SyncroPS] <> 1
            THEN
                UPDATE
                SET [Destination].[SyncroPS] = 1
                    ,[Destination].[SyncroLED] = GETDATE()
        OUTPUT $ACTION
        ) AS TheMerge(IUD)

    IF @Debug = 1
        SELECT IUD AS [Action]
            ,COUNT(IUD) AS [Count]
        FROM @Actions
        GROUP BY IUD
END

我无法确切地看到错误的位置。我试图在许多其他数据库上启动相同的脚本而不会出错...这真的让我很生气,我觉得问题可能是微不足道的,我真的很蠢,因为我找不到它...

这是另一个自动生成的过程,它不会产生任何错误(从另一个db生成):

CREATE PROCEDURE [dbo].[COMPUTE_Agente] (@Debug INT = 0)
AS
BEGIN
    DECLARE @Actions TABLE (IUD VARCHAR(100))

    INSERT INTO @Actions (IUD)
    SELECT IUD
    FROM (
        MERGE [frontier].[Agente] AS [Destination]
        USING [dbo].[Agente] AS [Source]
            ON [Destination].[CodiceAgente] = [Source].[CodiceAgente]
        WHEN NOT MATCHED BY TARGET
            THEN
                INSERT (
                    [CodiceAgente]
                    ,[Nome]
                    ,[CodiceFiscale]
                    ,[PartitaIVA]
                    ,[Provvigioni]
                    ,[Indirizzo]
                    ,[Localita]
                    ,[CAP]
                    ,[Provincia]
                    ,[Regione]
                    ,[Nazione]
                    ,[Telefono]
                    ,[Cellulare]
                    ,[Fax]
                    ,[EMail]
                    ,[SitoWeb]
                    ,[DataModifica]
                    ,[CHECKSUM]
                    ,[SyncroLED]
                    ,[SyncroPS]
                    )
                VALUES (
                    [Source].[CodiceAgente]
                    ,[Source].[Nome]
                    ,[Source].[CodiceFiscale]
                    ,[Source].[PartitaIVA]
                    ,[Source].[Provvigioni]
                    ,[Source].[Indirizzo]
                    ,[Source].[Localita]
                    ,[Source].[CAP]
                    ,[Source].[Provincia]
                    ,[Source].[Regione]
                    ,[Source].[Nazione]
                    ,[Source].[Telefono]
                    ,[Source].[Cellulare]
                    ,[Source].[Fax]
                    ,[Source].[EMail]
                    ,[Source].[SitoWeb]
                    ,[Source].[DataModifica]
                    ,BINARY_CHECKSUM([Source].[CodiceAgente], [Source].[Nome], [Source].[CodiceFiscale], [Source].[PartitaIVA], [Source].[Provvigioni], [Source].[Indirizzo], [Source].[Localita], [Source].[CAP], [Source].[Provincia], [Source].[Regione], [Source].[Nazione], [Source].[Telefono], [Source].[Cellulare], [Source].[Fax], [Source].[EMail], [Source].[SitoWeb], [Source].[DataModifica])
                    ,GETDATE()
                    ,0
                    )
        WHEN MATCHED
            AND [Destination].[CHECKSUM] <> BINARY_CHECKSUM([Source].[CodiceAgente], [Source].[Nome], [Source].[CodiceFiscale], [Source].[PartitaIVA], [Source].[Provvigioni], [Source].[Indirizzo], [Source].[Localita], [Source].[CAP], [Source].[Provincia], [Source].[Regione], [Source].[Nazione], [Source].[Telefono], [Source].[Cellulare], [Source].[Fax], [Source].[EMail], [Source].[SitoWeb], [Source].[DataModifica])
            THEN
                UPDATE
                SET [Destination].[CodiceAgente] = [Source].[CodiceAgente]
                    ,[Destination].[Nome] = [Source].[Nome]
                    ,[Destination].[CodiceFiscale] = [Source].[CodiceFiscale]
                    ,[Destination].[PartitaIVA] = [Source].[PartitaIVA]
                    ,[Destination].[Provvigioni] = [Source].[Provvigioni]
                    ,[Destination].[Indirizzo] = [Source].[Indirizzo]
                    ,[Destination].[Localita] = [Source].[Localita]
                    ,[Destination].[CAP] = [Source].[CAP]
                    ,[Destination].[Provincia] = [Source].[Provincia]
                    ,[Destination].[Regione] = [Source].[Regione]
                    ,[Destination].[Nazione] = [Source].[Nazione]
                    ,[Destination].[Telefono] = [Source].[Telefono]
                    ,[Destination].[Cellulare] = [Source].[Cellulare]
                    ,[Destination].[Fax] = [Source].[Fax]
                    ,[Destination].[EMail] = [Source].[EMail]
                    ,[Destination].[SitoWeb] = [Source].[SitoWeb]
                    ,[Destination].[DataModifica] = [Source].[DataModifica]
                    ,[Destination].[CHECKSUM] = BINARY_CHECKSUM([Source].[CodiceAgente], [Source].[Nome], [Source].[CodiceFiscale], [Source].[PartitaIVA], [Source].[Provvigioni], [Source].[Indirizzo], [Source].[Localita], [Source].[CAP], [Source].[Provincia], [Source].[Regione], [Source].[Nazione], [Source].[Telefono], [Source].[Cellulare], [Source].[Fax], [Source].[EMail], [Source].[SitoWeb], [Source].[DataModifica])
                    ,[Destination].[SyncroLED] = GETDATE()
                    ,[Destination].[SyncroPS] = 0
        WHEN NOT MATCHED BY SOURCE
            AND [Destination].[SyncroPS] <> 1
            THEN
                UPDATE
                SET [Destination].[SyncroPS] = 1
                    ,[Destination].[SyncroLED] = GETDATE()
        OUTPUT $ACTION
        ) AS TheMerge(IUD)

    IF @Debug = 1
        SELECT IUD AS [Action]
            ,COUNT(IUD) AS [Count]
        FROM @Actions
        GROUP BY IUD
END

提前感谢您提供给我的任何帮助

1 个答案:

答案 0 :(得分:2)

您的代码中没有语法错误。数据库的兼容级别为SQL Server 2005 (90)或更低。它必须是SQL Server 2008 (100)或更高。

ALTER DATABASE Compatibility Level (Transact-SQL)