3

我有一个使用新的合并输出子句的脚本。我已经在 3 个不同的实例(所有非生产环境)中运行它并且效果很好。当我尝试在我们的生产环境中运行它时,我得到了错误:

以用户身份执行:xxx\xxx。“合并”附近的语法不正确。您可能需要将当前数据库的兼容级别设置为更高的值才能启用此功能。请参阅 ALTER DATABASE 的 SET COMPATIBILITY_LEVEL 选项的帮助。[SQLSTATE 42000](错误 325)“合并”附近的语法不正确。您可能需要将当前数据库的兼容级别设置为更高的值才能启用此功能。请参阅 ALTER DATABASE 的 SET COMPATIBILITY_LEVEL 选项的帮助。[SQLSTATE 42000](错误 325)。步骤失败。

我检查了每个实例的版本,它们都是 10.0.4000.0。所有非系统数据库都设置为兼容级别 90 (2005),系统数据库设置为 100 (2008)。我还需要检查什么以查看我的生产实例与其他非生产实例的不同之处?

这是查询:

Declare @user varchar(20),
        @message varchar(max)
Set     @user = 'ISS20120917-144'

Create Table #data
(
    CustomerEventID_Surrogate Int Identity (1,1) Not Null Primary Key,
    CustomerNumber Int Not Null,
    ConvictionEventID Int Not Null,
    CustomerEventHierarchyID Int Not Null,
    SanctionEventID Int Not Null,
    ReferenceNumber varchar(40) Null,
    ConvictionACDID Int Null,
    State_Code varchar(2) Not Null,
    County_ID Int Null,
    CitationCDLHolderValueID Int Null,
    Hazmat Bit Null,
    CMV Bit Null,
    PassengerEndorsement Bit Null,
    OccurrenceDate DateTime Not Null,
    ConvictionDate DateTime Not Null,
    CourtOrder Bit Null
)

Create Table #surrogatemap
(
    CustomerEventID_Surrogate Int Not Null,
    NewCustomerEventID Int Not Null
)

Create Table #surrogateHIDmap
(
    NewCustomerEventID Int Not Null,
    NewHistoryEventDetailID Int Not Null
)

Begin Tran

Begin Try
    Insert Into #data
    Select  ce.Cust_No,
            ce.CustomerEventID,
            ceh.CustomerEventHierarchyID,
            ceSAN.CustomerEventID,
            ce.ReferenceNumber,
            hed.ACDID,
            hed.State_Code,
            hed.County_ID,
            hed.CitationCDLHolderValueID,
            hed.Hazmat,
            hed.CMV,
            hed.PassengerEndorsement,
            hed.OccurrenceDate,
            Case    When cd.ConvictionDate IS NOT NULL Then cd.ConvictionDate
                    Else hed.OccurrenceDate
            End As [ConvictionDate],
            hed.CourtOrder
    From IADS..CustomerEvent ce
    Inner Join IADS..HistoryEventDetail hed On hed.CustomerEventID = ce.CustomerEventID
        And hed.EndDate IS NULL
    Inner Join IADS..CustomerEventCode cec On cec.CustomerEventCodeID = hed.CustomerEventCodeID
        And cec.CustomerEventCodeID <> -51
    Left Outer Join IADS..ConvictionDetail cd On cd.HistoryEventDetailID = hed.HistoryEventDetailID
    Inner Join IADS..CustomerEventHierarchy ceh On ceh.CustomerEventID = ce.CustomerEventID
        And ceh.EndDate IS NULL
    Inner Join IADS..CustomerEvent ceSAN On ceSAN.CustomerEventID = ceh.RelatedCustomerEventID
        And ceSAN.CustomerEventDispositionID IS NULL
    Inner Join IADS..CustomerSanctionDetail csd On csd.CustomerEventID = ceSAN.CustomerEventID
        And csd.SanctionDiscardedReasonID IS NULL
    Inner Join IADS..SanctionReasonCode src On src.SanctionReasonCodeID = csd.SanctionReasonCodeID
        And src.SanctionReasonCodeID = -320
    Where ce.CustomerEventDispositionID IS NULL

    Merge Into IADS..CustomerEvent
        Using #data As src On 1 = 0
    When Not Matched Then
    Insert
    (
        CustomerEventCategoryID,
        Cust_No,
        ReferenceNumber,
        CreatedBy,
        CreatedDate,
        UpdatedBy,
        UpdatedDate
    )
    Values
    (
        -2,
        src.CustomerNumber,
        src.ReferenceNumber,
        @user,
        GetDate(),
        @user,
        GetDate()
    )
    Output
        src.CustomerEventID_Surrogate,
        inserted.CustomerEventID
    Into #surrogatemap;

    Select  sm.NewCustomerEventID,
        -8 As [HistoryEventTypeID],
        -51 As [CustomerEventCodeID],
        131 As [ACDID],
        d.State_Code,
        d.County_ID,
        d.CitationCDLHolderValueID,
        d.OccurrenceDate,
        d.ConvictionDate,
        d.Hazmat,
        d.CMV,
        d.CourtOrder,
        GETDATE() As [EffectiveDate],
        @user As [UpdatedBy],
        GETDATE() As [UpdatedDate],
        d.ConvictionACDID,
        d.PassengerEndorsement
    Into    #hiddata
    From    #data d
    Inner Join #surrogatemap sm On sm.CustomerEventID_Surrogate = d.CustomerEventID_Surrogate

    Merge Into IADS..HistoryEventDetail
        Using #hiddata As src On 1 = 0
    When Not Matched Then
    Insert
    (
        CustomerEventID,
        HistoryEventTypeID,
        CustomerEventCodeID,
        ACDID,
        State_Code,
        County_ID,
        CitationCDLHolderValueID,
        OccurrenceDate,
        Hazmat,
        CMV,
        CourtOrder,
        EffectiveDate,
        UpdatedBy,
        UpdatedDate,
        UnderlyingACDID,
        PassengerEndorsement
    )
    Values
    (
        src.NewCustomerEventID,
        src.HistoryEventTypeID,
        src.CustomerEventCodeID,
        src.ACDID,
        src.State_Code,
        src.County_ID,
        src.CitationCDLHolderValueID,
        src.OccurrenceDate,
        src.Hazmat,
        src.CMV,
        src.CourtOrder,
        src.EffectiveDate,
        src.UpdatedBy,
        src.UpdatedDate,
        src.ConvictionACDID,
        src.PassengerEndorsement
    )
    Output
        src.NewCustomerEventID,
        inserted.HistoryEventDetailID
    Into #surrogateHIDmap;

    Insert Into IADS..CustomerEventHierarchy
    (
        CustomerEventID,
        RelatedCustomerEventID,
        EffectiveDate,
        UpdatedBy,
        UpdatedDate
    )
    Select  sm.NewCustomerEventID,
            d.SanctionEventID,
            GETDATE(),
            @user,
            GETDATE()
    From    #data d
    Inner Join #surrogatemap sm On sm.CustomerEventID_Surrogate = d.CustomerEventID_Surrogate

    Insert Into IADS..CourtFineDetail
    (
        HistoryEventDetailID,
        ConvictionDate
    )
    Select  s.NewHistoryEventDetailID,
            d.ConvictionDate
    From  #hiddata d
    Inner Join #surrogateHIDmap s On s.NewCustomerEventID = d.NewCustomerEventID

    -- Remove the tie to the SUS077
    Update IADS..CustomerEventHierarchy
    Set     EndDate = GETDATE(),
            UpdatedBy = @user,
            UpdatedDate = GETDATE()
    Where   CustomerEventHierarchyID In (Select CustomerEventHierarchyID From #data)

    -- Build temp table containing the records that have already purged
    Select  ce.Cust_No,
            ce.CustomerEventID,
            ceh.CustomerEventHierarchyID
    Into    #disposedRecords
    From    IADS..CustomerEvent ce
    Inner Join IADS..HistoryEventDetail hed On hed.CustomerEventID = ce.CustomerEventID
        And hed.EndDate IS NULL
    Inner Join IADS..CustomerEventCode cec On cec.CustomerEventCodeID = hed.CustomerEventCodeID
        And hed.CustomerEventCodeID <> -51
    Inner Join IADS..CustomerEventHierarchy ceh On ceh.CustomerEventID = ce.CustomerEventID
        And ceh.EndDate IS NULL
    Inner Join IADS..CustomerEvent ceSAN On ceSAN.CustomerEventID = ceh.RelatedCustomerEventID
        And ceSAN.CustomerEventDispositionID IS NOT NULL
    Inner Join IADS..CustomerSanctionDetail csd On csd.CustomerEventID = ceSAN.CustomerEventID
        And csd.SanctionReasonCodeID = -320
    Where   ce.CustomerEventDispositionID IS NOT NULL
    Order By ce.CustomerEventDispositionDate Desc

    -- Un-purge all of the records that were previously tied to a SUS077
    Update  IADS..CustomerEvent
    Set     CustomerEventDispositionID = Null,
            CustomerEventDispositionComment = Null,
            CustomerEventDispositionDate = Null,
            UpdatedBy = @user,
            UpdatedDate = GETDATE()
    Where   CustomerEventID In (Select CustomerEventID From #disposedRecords)

    -- Remove the records from the PURGEEventsReadyForPurge table
    Delete
    From IADS..PURGEEventsReadyForPurge
    Where CustomerEventID In (Select CustomerEventID From #disposedRecords)

    -- Remove tie of purged records
    Update  IADS..CustomerEventHierarchy
            Set EndDate = GETDATE(),
            UpdatedBy = @user,
            UpdatedDate = GETDATE()
    Where   CustomerEventHierarchyID In (Select CustomerEventHierarchyID From #disposedRecords)

    Delete From IADS..PURGEEventsReadyForPurge Where PURGEEventsReadyForPurgeID In
    (
        Select  PURGEEventsReadyForPurgeID
        From    IADS..PURGEEventsReadyForPurge p
        Inner Join IADS..CustomerEvent ce On ce.CustomerEventID = p.CustomerEventID
            And ce.CustomerEventDispositionID IS NULL
        Inner Join IADS..CustomerEventCategory ceg On ceg.CustomerEventCategoryID = ce.CustomerEventCategoryID
        Left Outer Join IADS..CustomerEventHierarchy ceh On ceh.CustomerEventID = ce.CustomerEventID
        Left Outer Join IADS..CustomerEventHierarchy ceh2 On ceh2.RelatedCustomerEventID = ce.CustomerEventID
        Where   p.PurgeDate IS NOT NULL
    )

    Drop Table #disposedRecords
    Drop Table #hiddata
    Drop Table #surrogateHIDmap
    Drop Table #surrogatemap
    Drop Table #data

    Commit
End Try
Begin Catch
    Drop Table #disposedRecords
    Drop Table #hiddata
    Drop Table #surrogateHIDmap
    Drop Table #surrogatemap
    Drop Table #data

    Rollback
End Catch
4

1 回答 1

6

您可以尝试这两种方法中的任何一种。
1. 将兼容性级别更新为 100。

ALTER DATABASE [dbname] SET COMPATIBILITY_LEVEL = 100  

2. MERGE 语句和 MERGE 之前的语句以分号 (;) 结束

希望它有效。

于 2012-09-25T18:56:39.500 回答