1

我正在为我的公司建立一个假期跟踪网站。我想为新的假期请求设置一个电子邮件系统。

我有两个表用外键连接。

员工:

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,

假期申请表:

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。

电子邮件必须发送给在员工表中输入的员工区域经理。

电子邮件正文必须引用休假的员工,当然还有假期本身的详细信息。

4

1 回答 1

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'

要使用没有查询对象的 Body:

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'
于 2019-01-25T13:35:38.337 回答