跨服务器使用SQL触发器发生死锁
I used an Insert-trigger on the Table-A of the Database-A on Server-A. This trigger will put the Inserted-data into Table-B of the Database-B on Server-B.
I used an Insert-trigger on the Table-B of the Database-B on Server-B. This trigger uses the Inserted-data to calculate, and then insert the result in the Table-C of the Database-B on Server-B.
I used an Update-trigger on the Table-C of the Database-B on Server-B. This trigger used the Inserted-data to calculate, and then send mail automatically.
SQL Server now looks as if often deadlock. Is there any good solution?
Trigger on Table-A of Database-A on Server-A as below:
USE [Database-A]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[TRIGGER_INSERT_A]
ON [dbo].[Table-A]
FOR INSERT
AS
BEGIN
INSERT INTO [Server-B].[Database-B].[dbo].[Table-B] (
[Server-B].[dbo].[Table-B].[field1],
[Server-B].[dbo].[Table-B].[field2],
[Server-B].[dbo].[Table-B].[field3],
[Server-B].[dbo].[Table-B].[field4],
[Server-B].[dbo].[Table-B].[field5],
[Server-B].[dbo].[Table-B].[field6],
[Server-B].[dbo].[Table-B].[field7]
) SELECT
[field1],
[field2],
[field3],
[field4],
[field5],
[field6],
[field7]
FROM inserted
END
GO
Trigger on Table-B of Database-B on Server-B as below:
USE [Database-B]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[TRIGGER_INSERT_B]
ON [dbo].[Table-B]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
-- Logic portion is omitted
-- UPDATE [dbo].[Table-B] OR INSERT [dbo].[Table-B]
END
GO
Trigger on Table-C of Database-B on Server-B as below:
USE [Database-B]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[TRIGGER_UPDATE_C]
ON [dbo].[Table-C]
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE @recipients VARCHAR(MAX)
DECLARE @subject NVARCHAR(255)
DECLARE @body NVARCHAR(MAX)
DECLARE @body_format VARCHAR(20)
DECLARE @importance VARCHAR(6)
DECLARE @sensitivity VARCHAR(12)
DECLARE @query NVARCHAR(MAX)
DECLARE @execute_query_database NVARCHAR(128)
DECLARE @attach_query_result_as_file BIT
DECLARE @query_attachment_filename NVARCHAR(260)
DECLARE @query_result_header BIT
DECLARE @query_result_width INT
DECLARE @query_result_separator CHAR(1)
DECLARE @exclude_query_output BIT
DECLARE @append_query_error BIT
DECLARE @query_no_truncate BIT
SELECT
@recipients = [recipients],
@subject = [subject],
@body = [body],
@body_format = [body_format],
@importance = [importance],
@sensitivity = [sensitivity]
FROM [dbo].[Table-D]
SET @query = 'query portion is omitted'
SET @execute_query_database = 'Database-B'
SET @attach_query_result_as_file = 1
SET @query_attachment_filename = 'Automatic alarm.txt'
SET @query_result_header = 1
SET @query_result_width = 256
SET @query_result_separator = ' '
SET @exclude_query_output = 0
SET @append_query_error = 0
SET @query_no_truncate = 0
-- UPDATE [dbo].[Table-C] here
EXEC [msdb].[dbo].[sp_send_dbmail]
@profile_name = 'profile_name',
@recipients = @recipients,
@subject = @subject,
@body = @body,
@body_format = @body_format,
@importance = @importance,
@sensitivity = @sensitivity,
@query = @query,
@execute_query_database = @execute_query_database,
@attach_query_result_as_file = @attach_query_result_as_file,
@query_attachment_filename = @query_attachment_filename,
@query_result_header = @query_result_header,
@query_result_width = @query_result_width,
@query_result_separator = @query_result_separator,
@exclude_query_output = @exclude_query_output,
@append_query_error = @append_query_error,
@query_no_truncate = @query_no_truncate
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Found answer here. FYI