SQL Server:无法通过SSMS编辑数据

时间:2017-05-11 14:13:32

标签: sql-server view triggers ssms

我处于一种奇怪的情况,我无法通过SSMS更新视图中的行。我收到这个错误:

  

错误来源:SQL Server管理数据工具
  错误消息:更新或删除的行值不会使该行唯一或它们会更改多行

但是,我可以在Visual Studio中更新它们,也可以通过TSQL Update语句更新它们。这看起来像是SQL Server Management Studio中的一个错误。

感谢任何帮助/建议。

查看

CREATE VIEW [ELog].[FileDroppingArea_2]
AS
       SELECT
                ea.SK_EmailAttachementsId,
                e.SK_EmailId,
                e.ServiceTypeCode,
                e.serviceprovidercode,
                pa.OrginalNameOfAttachment AS ParentAttachmentOriginalName,
                pa.NewNameOfAttachment AS ParentAttachmentName,
                e.FileNameModifier,
                ea.OrginalNameOfAttachment,
                --ea.NewFileName,
       (
              SELECT
                       ServiceTypeSplit.FieldValue
              FROM Fnc.mTVFSplitStringIntoTable_MT(ea.NewFileName, '_') ServiceTypeSplit
              WHERE ServiceTypeSplit.RowId = '1'
       ) AS ServiceTypeFromFile,
       (
              SELECT
                       ServiceProviderSplit.FieldValue
              FROM Fnc.mTVFSplitStringIntoTable_MT(ea.NewFileName, '_') ServiceProviderSplit
              WHERE ServiceProviderSplit.RowId = '2'
       ) AS ServiceProviderFromFile,
       (
              SELECT
                       SubmissionYearSplit.FieldValue
              FROM Fnc.mTVFSplitStringIntoTable_MT(ea.NewFileName, '_') SubmissionYearSplit
              WHERE SubmissionYearSplit.RowId = '3'
       ) AS SubmissionYearFromFile,
       (
              SELECT
                       SubmissionMonthSplit.FieldValue
              FROM Fnc.mTVFSplitStringIntoTable_MT(ea.NewFileName, '_') SubmissionMonthSplit
              WHERE SubmissionMonthSplit.RowId = '4'
       ) AS SubmissionMonthFromFile,
       (
              SELECT
                       CommissionerCodeSplit.FieldValue
              FROM Fnc.mTVFSplitStringIntoTable_MT(ea.NewFileName, '_') CommissionerCodeSplit
              WHERE CommissionerCodeSplit.RowId = '5'
       ) AS CommissionerCodeFromFile,
       (
              SELECT
                       FlexOrFreezeeSplit.FieldValue
              FROM Fnc.mTVFSplitStringIntoTable_MT(ea.NewFileName, '_') FlexOrFreezeeSplit
              WHERE FlexOrFreezeeSplit.RowId = '6'
       ) AS FlexOrFreezeeFromFile,
       (
              SELECT
                       VersionSplit.FieldValue
              FROM Fnc.mTVFSplitStringIntoTable_MT(ea.NewFileName, '_') VersionSplit
              WHERE VersionSplit.RowId = '7'
       ) AS VersionFromFile,
       (
              SELECT
                       ServiceSubTypeSplit.FieldValue
              FROM Fnc.mTVFSplitStringIntoTable_MT(ea.NewFileName, '_') ServiceSubTypeSplit
              WHERE ServiceSubTypeSplit.RowId = '8'
       ) AS ServiceSubTypeFromFile,
                ea.CopyToServiceType
       FROM ELog.EmailAttachements ea
                INNER JOIN
                ELog.Emails e ON e.SK_EmailId = ea.SK_EmailId
                LEFT JOIN
                ELog.EmailAttachements pa ON ea.SK_ParentEmailAttachements = pa.SK_EmailAttachementsId
       WHERE ea.IsReadyForProcessing = 1
                 AND ea.IsMovedToRaw = 0

而不是更新触发器

CREATE TRIGGER [ELog].[TRG_upd_FileDropAreaByColumn] ON [ELog].[FileDroppingArea_2]
INSTEAD OF UPDATE
AS
BEGIN
    DECLARE @SKEmailAttachmentId INT
    DECLARE @ServiceTypeFromFile VARCHAR(MAX)
    DECLARE @ServiceProviderFromFile VARCHAR(MAX)
    DECLARE @SubmissionYearFromFile VARCHAR(MAX)
    DECLARE @SubmissionMonthFromFile VARCHAR(MAX)
    DECLARE @CommissionerCodeFromFile VARCHAR(MAX)
    DECLARE @FlexOrFreezeeFromFile VARCHAR(MAX)
    DECLARE @VersionFromFile VARCHAR(MAX)
    DECLARE @ServiceSubTypeFromFile VARCHAR(MAX)
    DECLARE @CopyToServiceType VARCHAR(MAX)

    DECLARE @NewFileName VARCHAR(MAX)
    DECLARE @CalculatedVersion VARCHAR(MAX)
    DECLARE @CurrentFileName VARCHAR(MAX)
    DECLARE @ServiceType_Current VARCHAR(MAX)
    DECLARE @ServiceProvider_Current VARCHAR(MAX)
    DECLARE @SubmissionYear_Current VARCHAR(MAX)
    DECLARE @SubmissionMonth_Current VARCHAR(MAX)
    DECLARE @CommissionerCode_Current VARCHAR(MAX)
    DECLARE @FlexOrFreezee_Current VARCHAR(MAX)
    DECLARE @Version_Current VARCHAR(MAX)
    DECLARE @ServiceSubType_Current VARCHAR(MAX)

    DECLARE InsteadOfUpdate CURSOR LOCAL FAST_FORWARD FOR 
        SELECT
            I.SK_EmailAttachementsId,
            I.ServiceTypeFromFile,
            I.ServiceProviderFromFile,
            I.SubmissionYearFromFile,
            I.SubmissionMonthFromFile,
            I.CommissionerCodeFromFile,
            I.FlexOrFreezeeFromFile,
            I.VersionFromFile,
            I.ServiceSubTypeFromFile,
            I.CopyToServiceType
        FROM 
            INSERTED I
        INNER JOIN
            DELETED D ON D.SK_EmailAttachementsId = I.SK_EmailAttachementsId
        WHERE 
            NOT(ISNULL(I.ServiceTypeFromFile, '') COLLATE latin1_general_cs_as = ISNULL(D.ServiceTypeFromFile, '') COLLATE latin1_general_cs_as
            AND ISNULL(I.ServiceProviderFromFile, '') COLLATE latin1_general_cs_as = ISNULL(D.ServiceProviderFromFile, '') COLLATE latin1_general_cs_as
            AND ISNULL(I.SubmissionYearFromFile, '') COLLATE latin1_general_cs_as = ISNULL(D.SubmissionYearFromFile, '') COLLATE latin1_general_cs_as
            AND ISNULL(I.SubmissionMonthFromFile, '') COLLATE latin1_general_cs_as = ISNULL(D.SubmissionMonthFromFile, '') COLLATE latin1_general_cs_as
            AND ISNULL(I.CommissionerCodeFromFile, '') COLLATE latin1_general_cs_as = ISNULL(D.CommissionerCodeFromFile, '') COLLATE latin1_general_cs_as
            AND ISNULL(I.FlexOrFreezeeFromFile, '') COLLATE latin1_general_cs_as = ISNULL(D.FlexOrFreezeeFromFile, '') COLLATE latin1_general_cs_as
            AND ISNULL(I.VersionFromFile, '') COLLATE latin1_general_cs_as = ISNULL(D.VersionFromFile, '') COLLATE latin1_general_cs_as
            AND ISNULL(I.ServiceSubTypeFromFile, '') COLLATE latin1_general_cs_as = ISNULL(D.ServiceSubTypeFromFile, '') COLLATE latin1_general_cs_as
            AND ISNULL(I.CopyToServiceType, '') COLLATE latin1_general_cs_as = ISNULL(D.CopyToServiceType, '') COLLATE latin1_general_cs_as)

    OPEN InsteadOfUpdate

    FETCH NEXT FROM InsteadOfUpdate INTO @SKEmailAttachmentId, @ServiceTypeFromFile, @ServiceProviderFromFile, @SubmissionYearFromFile,                                                                                  @SubmissionMonthFromFile, @CommissionerCodeFromFile, @FlexOrFreezeeFromFile, @VersionFromFile, @ServiceSubTypeFromFile, @CopyToServiceType 

    WHILE @@FETCH_STATUS = 0
    BEGIN


                           SET @CurrentFileName =
                           (
                                  SELECT
                                           ea.NewFileName
                                  FROM ELog.EmailAttachements ea
                                  WHERE ea.SK_EmailAttachementsId = @SKEmailAttachmentId
                           )


                           SELECT
                                    @ServiceType_Current = ServiceTypeSplit.FieldValue
                           FROM Fnc.mTVFSplitStringIntoTable_MT(@CurrentFileName, '_') ServiceTypeSplit
                           WHERE ServiceTypeSplit.RowId = '1'

                           SELECT
                                    @ServiceProvider_Current = ServiceProviderSplit.FieldValue
                           FROM Fnc.mTVFSplitStringIntoTable_MT(@CurrentFileName, '_') ServiceProviderSplit
                           WHERE ServiceProviderSplit.RowId = '2'

                           SELECT
                                    @SubmissionYear_Current = SubmissionYearSplit.FieldValue
                           FROM Fnc.mTVFSplitStringIntoTable_MT(@CurrentFileName, '_') SubmissionYearSplit
                           WHERE SubmissionYearSplit.RowId = '3'

                           SELECT
                                    @SubmissionMonth_Current = SubmissionMonthSplit.FieldValue
                           FROM Fnc.mTVFSplitStringIntoTable_MT(@CurrentFileName, '_') SubmissionMonthSplit
                           WHERE SubmissionMonthSplit.RowId = '4'

                           SELECT
                                    @CommissionerCode_Current = CommissionerCodeSplit.FieldValue
                           FROM Fnc.mTVFSplitStringIntoTable_MT(@CurrentFileName, '_') CommissionerCodeSplit
                           WHERE CommissionerCodeSplit.RowId = '5'

                           SELECT
                                    @FlexOrFreezee_Current = FlexOrFreezeeSplit.FieldValue
                           FROM Fnc.mTVFSplitStringIntoTable_MT(@CurrentFileName, '_') FlexOrFreezeeSplit
                           WHERE FlexOrFreezeeSplit.RowId = '6'

                           SELECT
                                    @Version_Current = VersionSplit.FieldValue
                           FROM Fnc.mTVFSplitStringIntoTable_MT(@CurrentFileName, '_') VersionSplit
                           WHERE VersionSplit.RowId = '7'

                           SELECT
                                    @ServiceSubType_Current = ServiceSubTypeSplit.FieldValue
                           FROM Fnc.mTVFSplitStringIntoTable_MT(@CurrentFileName, '_') ServiceSubTypeSplit
                           WHERE ServiceSubTypeSplit.RowId = '8';
                           WITH CurrentVersion
                                    AS (SELECT
                                           (
                                                  SELECT
                                                          VersionSplit.FieldValue
                                                  FROM Fnc.mTVFSplitStringIntoTable_MT(ea.NewFileName, '_') VersionSplit
                                                  WHERE VersionSplit.RowId = '7'
                                           ) StoredVersion
                                           FROM ELog.EmailAttachements ea
                                           WHERE ea.SK_EmailAttachementsId <> @SKEmailAttachmentId
                                                       AND @ServiceTypeFromFile =
                                           (
                                                  SELECT
                                                          ServiceTypeSplit.FieldValue
                                                  FROM Fnc.mTVFSplitStringIntoTable_MT(ea.NewFileName, '_') ServiceTypeSplit
                                                  WHERE ServiceTypeSplit.RowId = '1'
                                           )
                                    AND @ServiceProviderFromFile =
                                           (
                                                  SELECT
                                                          ServiceProviderSplit.FieldValue
                                                  FROM Fnc.mTVFSplitStringIntoTable_MT(ea.NewFileName, '_') ServiceProviderSplit
                                                  WHERE ServiceProviderSplit.RowId = '2'
                                           )
                           AND @SubmissionYearFromFile =
                                           (
                                                  SELECT
                                                          SubmissionYearSplit.FieldValue
                                                  FROM Fnc.mTVFSplitStringIntoTable_MT(ea.NewFileName, '_') SubmissionYearSplit
                                                  WHERE SubmissionYearSplit.RowId = '3'
                                           )
              AND @SubmissionMonthFromFile =
                                           (
                                                  SELECT
                                                          SubmissionMonthSplit.FieldValue
                                                  FROM Fnc.mTVFSplitStringIntoTable_MT(ea.NewFileName, '_') SubmissionMonthSplit
                                                  WHERE SubmissionMonthSplit.RowId = '4'
                                           )
              AND @CommissionerCodeFromFile =
                                           (
                                                  SELECT
                                                          CommissionerCodeSplit.FieldValue
                                                  FROM Fnc.mTVFSplitStringIntoTable_MT(ea.NewFileName, '_') CommissionerCodeSplit
                                                  WHERE CommissionerCodeSplit.RowId = '5'
                                           )
              AND @FlexOrFreezeeFromFile =
                                           (
                                                  SELECT
                                                          FlexOrFreezeeSplit.FieldValue
                                                  FROM Fnc.mTVFSplitStringIntoTable_MT(ea.NewFileName, '_') FlexOrFreezeeSplit
                                                  WHERE FlexOrFreezeeSplit.RowId = '6'
                                           )
              AND @ServiceSubTypeFromFile =
                                           (
                                                  SELECT
                                                          ServiceSubTypeSplit.FieldValue
                                                  FROM Fnc.mTVFSplitStringIntoTable_MT(ea.NewFileName, '_') ServiceSubTypeSplit
                                                  WHERE ServiceSubTypeSplit.RowId = '8'
                                           ))
                                    SELECT
                                            @CalculatedVersion = ISNULL(
                                                                                           (
                                                                                                  SELECT
                                                                                                          'V'+RIGHT('00'+CAST(MAX(REPLACE(StoredVersion, 'V', ''))+1 AS
                                                                                                          VARCHAR(10)), 2)
                                                                                           ), 'V01')
                                    FROM CurrentVersion


                           SET @CurrentFileName = REPLACE(@CurrentFileName, @ServiceType_Current, @ServiceTypeFromFile)
                           SET @CurrentFileName = REPLACE(@CurrentFileName, @ServiceProvider_Current, @ServiceProviderFromFile)
                           SET @CurrentFileName = REPLACE(@CurrentFileName, @SubmissionYear_Current, @SubmissionYearFromFile)
                           SET @CurrentFileName = REPLACE(@CurrentFileName, @SubmissionMonth_Current, @SubmissionMonthFromFile)
                           SET @CurrentFileName = REPLACE(@CurrentFileName, @CommissionerCode_Current, @CommissionerCodeFromFile)
                           SET @CurrentFileName = REPLACE(@CurrentFileName, @FlexOrFreezee_Current, @FlexOrFreezeeFromFile)
                           SET @CurrentFileName = REPLACE(@CurrentFileName, @Version_Current, @CalculatedVersion)
                           SET @CurrentFileName = REPLACE(@CurrentFileName, @ServiceSubType_Current, @ServiceSubTypeFromFile)

                           SET @NewFileName = @CurrentFileName





                           UPDATE EA
                              SET
                                     EA.NewFileName = @CurrentFileName,
                                     EA.CopyToServiceType = @CopyToServiceType,
                                     EA.LastUpdatedTimestamp = GETDATE(),
                                     EA.ModifiedBYUser = SUSER_SNAME()
                           FROM ELog.EmailAttachements EA
                           WHERE
                                    IsReadyForProcessing = 1
                                    AND IsMovedToRaw = 0
                                    AND EA.SK_EmailAttachementsId = @SKEmailAttachmentId
                                    AND NOT(ISNULL(EA.NewFileName, '') COLLATE latin1_general_cs_as = ISNULL(@CurrentFileName, '') COLLATE
                                    latin1_general_cs_as
                                                  AND ISNULL(EA.CopyToServiceType, '') COLLATE latin1_general_cs_as = ISNULL(@CopyToServiceType, '')
                                                  COLLATE latin1_general_cs_as)



                           FETCH NEXT FROM InsteadOfUpdate INTO @SKEmailAttachmentId,
                                                                                           @ServiceTypeFromFile,
                                                                                           @ServiceProviderFromFile,
                                                                                           @SubmissionYearFromFile,
                                                                                           @SubmissionMonthFromFile,
                                                                                           @CommissionerCodeFromFile,
                                                                                           @FlexOrFreezeeFromFile,
                                                                                           @VersionFromFile,
                                                                                           @ServiceSubTypeFromFile,
                                                                                           @CopyToServiceType
                     END
       END


GO

P.S。之前我直接从视图更新newFileName列,因为我将列拆分为8个不同的列,这8个新列在Visual Studio中显示为禁用

1 个答案:

答案 0 :(得分:0)

我需要做的就是在触发开始时设置NOCOUNT OFF。