0

帮助!以下触发器在执行后会导致阻塞:

USE [Automation]
GO
/****** Object:  Trigger [dbo].[AfterInsertSendEmail]    Script Date: 08/06/2012 22:05:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      Chris Cannon
-- Create date: 20120806
-- Description: TBC
-- =============================================
ALTER TRIGGER [dbo].[AfterInsertSendEmail] 
   ON  [dbo].[Enquiry] 
   AFTER INSERT
AS 
BEGIN;
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for trigger here
    DECLARE @l_profile_name nvarchar(128) = 'Automation Enquiries';
    DECLARE @l_importance varchar(6) = 'High';
    DECLARE @l_sensitivity varchar(12) = 'Personal';
    DECLARE @l_recipients varchar(500) = 'a.n.other@somewhere.co.uk';
    DECLARE @l_copy_recipients varchar(500) = 'b.n.other@somewhere.co.uk';
    DECLARE @l_blind_copy_recipients varchar(500) = 'c.n.other@somewhere.co.uk';
    DECLARE @l_reply_to varchar(500) = (SELECT [EmailAddress] FROM [inserted]);
    DECLARE @l_subject nvarchar(255) = (SELECT 'New Enquiry From ' + [FirstName] + ' ' + [LastName] FROM [inserted]);
    DECLARE @l_body nvarchar(500) = 'TBC';
    DECLARE @l_query nvarchar(500) = 'SELECT TOP(1) * FROM [Automation].[dbo].[Enquiry] ORDER BY [Id] DESC';
    DECLARE @l_query_result_separator char(1) = CHAR(9);
    DECLARE @l_query_result_no_padding bit = 1;
    DECLARE @l_attach_query_result_as_file bit = 1;
    DECLARE @l_query_attachment_filename nvarchar(260) = (SELECT 'automation_enquiry_' + REPLACE(REPLACE(REPLACE(CONVERT(nvarchar(19), [DateSubmitted], 120), '-', ''), ' ', ''), ':', '') + '.csv' FROM [inserted]);
    DECLARE @l_append_query_error bit = 1;

    EXEC [msdb].[dbo].[sp_send_dbmail]
          @profile_name = @l_profile_name
        , @importance = @l_importance
        , @sensitivity = @l_sensitivity
        , @recipients = @l_recipients
        , @copy_recipients = @l_copy_recipients
        , @blind_copy_recipients = @l_blind_copy_recipients
        , @reply_to = @l_reply_to
        , @subject = @l_subject
        , @body = @l_body
        , @query = @l_query
        , @query_result_separator = @l_query_result_separator
        , @query_result_no_padding = @l_query_result_no_padding
        , @attach_query_result_as_file = @l_attach_query_result_as_file
        , @query_attachment_filename = @l_query_attachment_filename
        , @append_query_error = @l_append_query_error;

END;

据我所知,它看起来像这条线:

DECLARE @l_query nvarchar(500) = 'SELECT TOP(1) * FROM [Automation].[dbo].[Enquiry] ORDER BY [Id] DESC';

可能会阻塞某些东西或触发器本身会阻塞该语句。

我已经尝试了几件没有运气的事情。

请帮忙!

4

2 回答 2

1

尝试使用“WITH(NOLOCK)”

SELECT TOP(1) * FROM [Automation].[dbo].[Enquiry] WITH (NOLOCK) ORDER BY [Id] DESC
于 2012-08-06T21:24:35.653 回答
1

好吧,您正处于事务的中间,因此至少该行被锁定,然后您发送邮件,邮件在您锁定的同一张表上运行选择顶部...

您可以按照 losbear 的建议尝试 nolock ,但是看到您正在运行的查询可能是为了显示您刚刚插入的记录的详细信息,并且您已经在触发器中插入了可用的记录,为什么还要打扰 @query 呢。

就我个人而言,我从不喜欢在交易过程中发送邮件的想法。我宁愿将请求排队,提交事务,然后让一些“服务”进行邮寄。不过,这将是更多的工作。

于 2012-08-06T21:52:03.017 回答