Tsql 如何在触发器中正确使用sp_send_dbmail?

Tsql 如何在触发器中正确使用sp_send_dbmail?,tsql,stored-procedures,triggers,Tsql,Stored Procedures,Triggers,我在一个名为Project的表上创建了一个触发器,该查询已成功编译。但是,当我将数据插入表时,会出现一个错误,说明: Msg 14636, Level 16, State 1, Procedure sp_send_dbmail, Line 112 [Batch Start Line 139] No global profile is configured. Specify a profile name in the @profile_name parameter. 以及: Msg 3930,

我在一个名为Project的表上创建了一个触发器,该查询已成功编译。但是,当我将数据插入表时,会出现一个错误,说明:

Msg 14636, Level 16, State 1, Procedure sp_send_dbmail, Line 112 [Batch Start Line 139]
No global profile is configured. Specify a profile name in the @profile_name parameter.
以及:

Msg 3930, Level 16, State 1, Procedure sp_send_dbmail, Line 64 [Batch Start Line 139]
The current transaction cannot be committed and cannot support operations that write to the log file. Roll back the transaction.
我不完全熟悉sp_send_dbmail过程,但我尝试过让它在触发触发器时发送电子邮件给xxxxx@gmail.com.

--Create a Table to hold the project audit
CREATE TABLE [dbo].[ProjectAudit](
    projectId char(4) not null,
    projectName varchar(50) null, 
    fundedbudget decimal(16,2) null,
    firmFedID char(9) null,
    statusID varchar(25) null,
    projectTypeID char(4) null, 
    startDate date null,  
    projectedEndDate date null,
    projectManager char(8) null,
    dateTimeCreated smalldatetime null,
    operation varchar(50),
    userName varchar(50)
)
go

-- Project trigger
create TRIGGER trg_ProjectAudit
ON Project
After Insert,Delete,Update
AS
Begin
     declare @name varchar(50)
     declare @body varchar(100) 
     if exists(select projectId from inserted)
     BEGIN
         select @name = projectName
         from inserted
         set @name = @name + ' has been inputted into the project table.'

         INSERT INTO ProjectAudit 
         (projectId, projectName, fundedbudget, firmFedID, statusID, projectTypeID, 
         startDate, projectedEndDate, projectManager, operation, dateTimeCreated, userName)
         SELECT projectId, projectName, fundedbudget, firmFedID, statusID, projectTypeID, 
         startDate, projectedEndDate, projectManager, 'INSERT', getdate(), System_user
         FROM Inserted
         exec msdb.dbo.sp_send_dbmail @recipients = 'xxxxx@gmail.com', 
         @body = @name
     END

     if exists(select projectId from deleted)
     begin
        select @name = projectName
         from deleted
         set @name = @name + ' has been deleted from the project table.'

        INSERT INTO ProjectAudit 
         (projectId, projectName, fundedbudget, firmFedID, statusID, projectTypeID, 
         startDate, projectedEndDate, projectManager, operation, dateTimeCreated, userName)
         SELECT projectId, projectName, fundedbudget, firmFedID, statusID, projectTypeID, 
         startDate, projectedEndDate, projectManager, 'DELETE', getdate(), System_user
         FROM deleted
         exec msdb.dbo.sp_send_dbmail @recipients = 'xxxxx@gmail.com', 
         @body = @name  
     end



     --check if a column update
     if (update(projectid) or update(projectTypeID))
     begin
        print 'ProjectID or ProjectTypeID column was updated'
        exec msdb.dbo.sp_send_dbmail @recipients = 'xxx@gmail.com',
            @body = 'Data has been updated in the project table.'   
     end
End
GO

如果没有正确设置dbmail,它将不会发送电子邮件。你有个人资料设置吗<代码>EXEC msdb.dbo.sysmail\u help\u profile\u sp检查帐户
EXEC msdb.dbo.sysmail\u help\u account\u sp
然后检查它们是否链接<代码>EXEC msdb.dbo.sysmail\u help\u profileaccount\u sp如果所有设置都正确。在
@recipients
put
@profile\u name='yourprofilename',
之前,将
配置文件名添加到
sp\u send\u dbmail
中,您可能还希望在电子邮件中有一个
主题。在
@body
放置
@subject='Your subject Text'之前,
sp\u send\u dbmail命令是否在触发器之外工作?如果没有,则需要首先启用/配置电子邮件。