我正在为我的公司建立一个假期跟踪网站。 我想为新的假期请求设置电子邮件系统。
我有两个用foregin键连接的表。
员工:
CREATE TABLE [dbo].[Employee](
[EmployeeID] [int] NOT NULL,
[FullName] [nvarchar](50) NOT NULL,
[Email] [nvarchar](50) NOT NULL,
[StartDate] [date] NOT NULL,
[ShiftID] [int] NOT NULL,
[AreaID] [int] NOT NULL,
[DisciplineID] [int] NOT NULL,
[SiteID] [int] NOT NULL,
[ALCategory] [int] NOT NULL,
[HoursTaken] [int] NOT NULL,
[StudyLeaveEntitlement] [int] NOT NULL,
[StudyLeaveTaken] [int] NOT NULL,
[StudyLeaveRemaining] AS ([StudyLeaveEntitlement]-[StudyLeaveTaken]),
[ExamLeaveTaken] [int] NOT NULL,
[ForceMajeure] [int] NOT NULL,
[BereavementLeaveTaken] [int] NOT NULL,
[MaternityLeaveTaken] [int] NOT NULL,
[ParentalLeaveTaken] [int] NOT NULL,
[AdoptionLeaveTaken] [int] NOT NULL,
[ManagerEmail] [nvarchar](50) NULL,
[AreaManagerEmail] [nvarchar](50) NULL,
HolidayRequestForm:
CREATE TABLE [dbo].[HolidayRequestForm](
[RequestID] [int] IDENTITY(1,1) NOT NULL,
[EmployeeID] [int] NOT NULL,
[StartDate] [date] NOT NULL,
[FinishDate] [date] NOT NULL,
[HoursTaken] [int] NOT NULL,
[Comments] [nvarchar](256) NULL,
[YearCreated] [int] NOT NULL,
[MonthCreated] [int] NOT NULL,
[DayCreated] [int] NOT NULL,
[YearOfHoliday] AS (datepart(year,[StartDate])),
[Approved] [bit] NULL,
ALTER TABLE [dbo].[HolidayRequestForm] WITH CHECK ADD CONSTRAINT [MyTable_MyColumn_FK] FOREIGN KEY([EmployeeID])
REFERENCES [dbo].[Employee] ([EmployeeID])
GO
我已经设置了sql,因此它可以发送电子邮件,但是我不确定如何实现以下目标。这是我尝试过的。
Create trigger EmailForApproval on [dbo].[HolidayRequestForm]
after INSERT
as
begin
exec msdb.dbo.sp_send_dbmail
@profile_name='HolidayRequests',
@recipients= [AreaManagerEmail],
@body='Hi [ManagerEmail], [employee].[Email] has requested a holiday please forward this email to [ManagerEmail] with a reply of Accept or Decline Thank you',
@subject='New Holiday Request'
我希望这样,当员工提交假期请求时,电子邮件将发送给区域经理以接受或拒绝。他们通过将电子邮件转发给潜在客户经理来执行此操作,然后经理可以将列Approved
更改为true。
电子邮件必须发送到在employees表中输入的Employees Area Manager。
电子邮件正文必须引用休假的员工以及休假本身的详细信息。
答案 0 :(得分:0)
您可以使用send_dbmail中的查询对象:
exec msdb.dbo.sp_send_dbmail
@profile_name='HolidayRequests',
@recipients= [AreaManagerEmail],
@body='...This is replaced by the query object...',
@subject='New Holiday Request'
@query= 'SET NOCOUNT ON;SELECT
''Hi '' + [ManagerEmail] + '', '' + [employee].[Email] +
'' has requested a holiday please forward this email
to '' + [ManagerEmail] + '' with a reply of Accept or Decline Thank you.''
FROM YOURTABLE
WHERE YOURCONDITION
;SET NOCOUNT OFF'
要在没有查询对象的情况下使用正文:
Declare @MEm VarChar(25) = (Select ManagerEmail From YOURTABLE Where YOURCONDITION)
Declare @em VarChar(25) = (Select eMail From YOURTABLE Where YOURCONDITION)
Declare @vBody =
'Hi ' + @MEm + ', ' + @em +
' has requested a holiday please forward this email
to ' + @MEm + ' with a reply of Accept or Decline Thank you.'
,
exec msdb.dbo.sp_send_dbmail
@profile_name='HolidayRequests',
@recipients= [AreaManagerEmail],
@body = @vBody,
@subject='New Holiday Request'