SQL Server:无法通过 SSMS 编辑数据
Posted
技术标签:
【中文标题】SQL Server:无法通过 SSMS 编辑数据【英文标题】:SQL Server : Unable to edit data through SSMS 【发布时间】:2017-05-11 14:13:31 【问题描述】:我处于一种奇怪的情况,我无法通过 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
附:之前我直接从视图中更新 newFileName 列,因为我将该列拆分为 8 个不同的列,这 8 个新列在 Visual Studio 中显示为禁用
【问题讨论】:
天哪 ..... cursor 在 trigger 内......这就是 绝对最糟糕的事情你可以为你的表现做!你真的应该尝试找到一种方法来 AVOID 游标 - 最肯定是在触发器内部! 我不喜欢它们中的任何一个,但考虑到界面开发人员的紧迫性和缺乏,这是我能想到的最好的方法 【参考方案1】:真的不知道你更新了什么。
重要!如果视图引用了多个基表,则不能删除一行。您只能更新属于单个基表的列。
重要!如果视图引用了多个基表,则不能插入一行。
https://docs.microsoft.com/en-us/sql/relational-databases/views/modify-data-through-a-view
【讨论】:
这有什么关系,而不是尝试插入或删除。您可以在具有多个表的视图中进行插入,这就是触发器的用途。 ***.com/questions/18555917/… 试图更新生成视图的基础表【参考方案2】:我需要做的就是在触发器开始时设置 NOCOUNT OFF。
【讨论】:
以上是关于SQL Server:无法通过 SSMS 编辑数据的主要内容,如果未能解决你的问题,请参考以下文章
通过 SSMS 连接时 SQL Server 2012 集成服务失败
无法从 SSMS 连接到 SQL Server Express