我们的 Microsoft SQL Server 数据库中有一个通知系统。发生的情况是一个名为 NotifyOnPropertyChangedTrigger 的 SQL 触发器创建了一个文件。我们的客户端软件使用 .NET FileSystemWatcher 类来等待新文件出现,它会读取并获取新的属性设置。这很好用,除非涉及 SQL 复制。
问题是这样的:
因此,我们将更改插入到我们的主数据库(复制到其他 3 个 SQL Server DB)中,SQL 触发器检测到更改,并且在硬盘上正确创建了文件。我们的数据库复制正确地更新了其他 3 个 DQL 数据库以反映最新的更改。然而,触发器只是偶尔工作,这意味着文件并不总是在其他 3 个硬盘上创建。(应该是。总共 4 个文件:1 个在主数据库上创建,3 个在复制数据库上创建)
我们正在使用合并复制。
任何想法为什么会发生这种情况?
提前致谢。
这是触发器:
ALTER TRIGGER [dbo].[NotifyOnPropertyChangeTrigger]
ON [dbo].[PropertyValues]
AFTER INSERT,DELETE,UPDATE
AS
BEGIN
SET NOCOUNT ON
DECLARE @propertyValueID int
DECLARE PropertyValueCursor CURSOR FOR
SELECT DISTINCT [PropertyValueID]
FROM (SELECT [PropertyValueID] FROM inserted
UNION ALL
SELECT [PropertyValueID] FROM deleted) AS Combined
OPEN PropertyValueCursor
FETCH NEXT FROM PropertyValueCursor INTO @propertyValueID
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @propertyID int
DECLARE @profileID int
DECLARE @contractName nvarchar(255)
IF ((SELECT COUNT(*) FROM inserted WHERE [PropertyValueID] = @propertyValueID) > 0)
BEGIN
SELECT @propertyID = [PropertyID], @profileID = [ProfileID], @contractName = [ContractName]
FROM inserted
END
ELSE
BEGIN
SELECT @propertyID = [PropertyID], @profileID = [ProfileID], @contractName = [ContractName]
FROM deleted
END
DECLARE @propertyName nvarchar(255)
DECLARE @needToSendNotification bit
SELECT @propertyName = [PropertyName], @needToSendNotification = [NotifyOnUpdate]
FROM [Properties]
WHERE [PropertyID] = @propertyID
IF (@needToSendNotification = 1)
BEGIN
DECLARE @profileName nvarchar(50)
SELECT @profileName = [ProfileName]
FROM [Profiles]
WHERE [ProfileID] = @profileID
DECLARE @fileName nvarchar(600) -- Must be at least 50 + 1 + 255 + 1 + 255 + 19 + 4
SET @fileName = dbo.CleanStringForFileName(@profileName)
+ '-'
+ dbo.CleanStringForFileName(@contractName)
+ '-'
+ dbo.CleanStringForFileName(@propertyName)
+ '-'
+ dbo.GetUTCDateTimeStampWithSeparator('-')
+ '.txt'
DECLARE @commandString nvarchar(650) -- Must be at least 5 + 10 + 27 + 585
SET @commandString = 'echo ' + CAST(@propertyValueID AS nvarchar(10)) + ' >> C:\ClientNotifications\' + @fileName
EXEC master.dbo.xp_cmdshell @commandString, NO_OUTPUT
END
FETCH NEXT FROM PropertyValueCursor INTO @propertyValueID
END
CLOSE PropertyValueCursor
DEALLOCATE PropertyValueCursor
END