我已经触发了在插入记录时运行,它应首先检查当前时间,如果它超出特定的小时和分钟,它应触发触发器并应从表中获取数据并使用这些值电子邮件的主题和正文。
下面是触发器工作正常,但我需要添加时间条件,我试图根据插入表中的记录查询表,但我没有收到所需的电子邮件。
正如您所看到的那样,我尝试在主题中使用表格值,但我在主题中收到了SQL Server Message
的电子邮件。
我想要添加的另一件事是查询表并检查表中是否有INSERTED.USERID
,如果它不可用,则不需要触发该触发器。
USE [Attendance]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[after_insert_ATT_LOG] ON [dbo].[ATT_LOG]
FOR INSERT
AS
BEGIN TRY
DECLARE @PositionCode NVARCHAR(5), @FullNameE NVARCHAR(45), @FullNameE2 NVARCHAR(40), @Email NVARCHAR(50), @EDescription NVARCHAR(100), @ReportingToShortNo NVARCHAR(5), @ReportingtoFullNameE NVARCHAR(45), @ReportingEmail NVARCHAR(50)
Select @PositionCode=ED.POsitionCode, @FullNameE=EP.FullNameE,@FullNameE2=ED.FullNameE, @Email=ED.Email, @EDescription=EP.EDescription, @ReportingToShortNo=EP.ReportingToShortNo, @ReportingtoFullNameE=EP.ReportingtoFullNameE, @ReportingEmail=(Select Email
FROM HRSystem.dbo.employeedetails WHERE PositionCode= EP.ReportingToShortNo) FROM HRSystem.dbo.employeedetails ED, HRSystem.dbo.Position EP WHERE ED.PositionID = EP.PositionID AND ED.PositionCode = (Select INSERTED.USERID FROM INSERTED) COLLATE DATABASE_DEFAULT;
EXEC msdb.dbo.sp_send_dbmail
@recipients = 'abc@abc.com',
@profile_name = 'Alert',
@subject = @FullNameE,
@body = 'Test Alert';
END TRY
BEGIN CATCH
DECLARE @dummy int
SET @dummy = 1
END CATCH
答案 0 :(得分:2)
关于你的第二个问题
我想要添加的另一件事是查询表并检查表中是否有INSERTED.USERID,如果它不可用,则不需要触发该触发器。
在插入行后立即触发FOR INSERT
触发器。意思是,你要寻找的USERID
肯定会在表ATT_LOG
中。
至于你的第一期。请尝试下面的t-sql代码:
CREATE TRIGGER [dbo].[after_insert_ATT_LOG] ON [dbo].[ATT_LOG]
FOR INSERT
AS
BEGIN TRY
DECLARE @PositionCode NVARCHAR(5), @FullNameE NVARCHAR(45), @FullNameE2 NVARCHAR(40), @Email NVARCHAR(50), @EDescription NVARCHAR(100), @ReportingToShortNo NVARCHAR(5), @ReportingtoFullNameE NVARCHAR(45), @ReportingEmail NVARCHAR(50)
Select @PositionCode=ED.POsitionCode, @FullNameE=EP.FullNameE,@FullNameE2=ED.FullNameE
, @Email=ED.Email, @EDescription=EP.EDescription
, @ReportingToShortNo=EP.ReportingToShortNo
, @ReportingtoFullNameE=EP.ReportingtoFullNameE
, @ReportingEmail=
(Select Email
FROM HRSystem.dbo.employeedetails
WHERE PositionCode= EP.ReportingToShortNo)
FROM HRSystem.dbo.employeedetails ED, HRSystem.dbo.Position EP
WHERE ED.PositionID = EP.PositionID
AND ED.PositionCode =
(Select INSERTED.PositionCode FROM INSERTED) COLLATE DATABASE_DEFAULT;
EXEC msdb.dbo.sp_send_dbmail
@recipients = 'abc@abc.com',
@profile_name = 'Alert',
@subject = @FullNameE,
@body = 'Test Alert';
END TRY
BEGIN CATCH
DECLARE @dummy int
SET @dummy = 1
END CATCH