跨服务器使用SQL触发器发生死锁

发布于 2022-09-06 11:33:51 字数 3970 浏览 16 评论 0

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Found answer here. FYI

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文