我写了一个存储过程,可以发送夜间电子邮件
'Sent to: ' + @EmailAddress + ';
To Whom It May concern:<br><br>
On '+ @StartDate +' an XXXX was at '+ @EventTitle +' and these issues were identified:<br>
'+ @IssuesIdentified +' and marked as requiring your assistance to follow up with the concern.<br><br>
Thank you for your prompt attention to this matter.'
现在,我想在此电子邮件中添加部门名称。原因是我有23个部门收到此通知电子邮件。 该部门的9至23使用相同的电子邮件地址,但是此人收到电子邮件后,必须创建票证以通知相应的部门进行跟进。
这是我当前正在使用的电子邮件查询
DECLARE @email varchar(max);
;
With TS
as ( select MAX(ID) as Id
from dimAgencyEmail d
group by id
)
select @email = coalesce(@Email + ';', '') + EmailAddress
from dimAgencyEmail d
join TSC c on c.Id = d.Id
select @email;
这是要求
Select case FollowUpBy_DepartmentName
when 1
then (select emailaddress from dimAgencyEmail
where Id ='1' ) --- there are 23 Id
from TSC where Requires_O_FollowUp = 1
or Requires_O1_FollowUp = 1
and NotificationSentDate is null
我需要帮助编写查询
在我的电子邮件中,我想输入部门名称,以便收到此电子邮件的人知道哪个部门需要响应后续行动。 我遇到的困难是,创建此表格的人可以根据需要选择尽可能多的跟进。发送电子邮件时如果“要求官员”或“要求其他后续活动”为1并且“通知电子邮件”为null,则我需要发送包含所有已检查部门的电子邮件,我很难编写执行此操作的查询。我将尝试使用临时表,但如果可以的话,我想避免使用它。
Sent to: ' + @EmailAddress + ';
To Whom It May concern:<br><br>
On '+ @StartDate +' an XXXX was at '+ @EventTitle +' and these issues were identified:<br>
'+ @IssuesIdentified +' and marked as requiring your assistance to follow up with the concern.<br><br> For the 000 center please create a ticket for MO, '+ @DepartmentName".
Thank you for your prompt attention to this matter.'
对查询DepartmentName的任何帮助表示赞赏。
请不要提出我的问题,我是新手,我正在学习如何正确地提出问题。如果您无法回答,请给某人一个机会看看。
谢谢。
这是我完整的存储过程
答案 0 :(得分:1)
如果我的理解正确,那么问题是您正在向所有部门发送静态电子邮件,现在您有了一个多个部门的列表,需要将不同的电子邮件发送给这些部门。
假设是这种情况,请将非静态变量(电子邮件,部门和其他任何变量)放入临时表,然后遍历它们。像这样:
--DECLARE Variables
DECLARE @email varchar(255)
DECLARE @department varchar(255)
etc...
--Populate temp table with all email/dept combos
--(guessing at some logic here)
SELECT FollowUpBy_DepartmentName Dept, EmailAddress Email
INTO #DeptEmails
FROM TSC t
JOIN dimAgencyEmail e ON t.ID = e.ID --?
WHERE Requires_O_FollowUp = 1
OR Requires_O1_FollowUp = 1
AND NotificationSentDate is null --Make sure this OR logic is right..
然后:
WHILE EXISTS (SELECT 1 FROM #DeptEmails)
BEGIN
SET @Department = (SELECT TOP 1 Dept FROM #DeptEmails)
SET @Email = (SELECT email FROM #DeptEmails WHERE Dept = @Department)
--Form email and send here
--(Your email query + EXEC msdb.send_dbmail etc.)
--Remove department/email record now that it has been sent
DELETE FROM #DeptEmails WHERE Dept = @Department
--Loop will begin again until all department emails have been sent
END
答案 1 :(得分:1)
让我知道这是否有帮助。
-- DECLARE @EmailDetail TABLE (NO need for temp table)
--create ctblEmailDetail table and populate with these fields (FollowUpBy1/2,FollowUpByAutoTheft ...FollowUpByTransit )
-- And use this table to fetch the necessary followUps match up by joining it.
CREATE TABLE ctblEmailDetail
(
ID IDENTITY(1,1) TINYINT,
FollowUp_Desc VARCHAR(250)
ACTIVE BIT ,CREATEBY VARCHAR(20), CREATEDATE DATETIME
)
/*
--EXAMPLE
ID FollowUp_Desc
-------------------------
1 FollowUpBy1
2 FollowUpBy2
3 FollowUp2
4 FollowUpBySeniorAffairs
5 FollowUpBySolidWaste
...
n FollowUpByTransit
*/
DECLARE
@ID AS INT,
@DepartmentName varchar(255),
@EmailAddress NVARCHAR(max),
@StartDate Date,
@EventTitle varchar(255),
@NotificationSentDate Datetime2(7),
@RequiresOfficerFollowUp Bit,
@RequiresOtherFollowUp Bit,
@IssuesIdentified varchar(255),
@DepartmentName AS varchar(255),
@EmailAddress AS nvarchar(max),
@StartDate AS Date,
@EventTitle AS varchar(255),
@NotificationSentDate AS Datetime2(7),
---fields
@RequiresOfficerFollowUp AS varchar(255),
@RequiresOtherFollowUp AS varchar(255),
@IssuesIdentified AS varchar(255),
--email fields
@Body varchar(1000),
@Subject varchar(200)
SET @StartDate = convert(varchar,'2019-06-01 01:31:00.0000000',7)
SET @Subject = 'Comunity event follow up needed<br>' + CONVERT(varchar, GETDATE(), 1);
SELECT EventTitle
,StartDate
,IssuesIdentified
,RequiresOfficerFollowUp
,RequiresOtherFollowUp
-- CASE WHEN ED.ID = 1 THEN FollowUpByHorseMountedUnit
-- WHEN ED.ID = 2 THEN FollowUpByArmedRobbery
-- WHEN ED.ID = 3 THEN FollowUpByBurglary
-- WHEN ED.ID = 4 THEN FollowUpByAutoTheft
-- WHEN ED.ID = 5 THEN FollowUpByNarcotics
-- WHEN ED.ID = 6 THEN FollowUpByVice
-- WHEN ED.ID = 7 THEN FollowUpByGangs
-- WHEN ED.ID = 8 THEN FollowUpByMotorUnit
-- WHEN ED.ID = 9 THEN FollowUpByAnimalWelfare
-- WHEN ED.ID = 10 THEN FollowUpByAviation
-- WHEN ED.ID = 11 THEN FollowUpByCityCouncil
-- WHEN ED.ID = 12 THEN FollowUpByCivilianPoliceOversightAgency
-- WHEN ED.ID = 13 THEN FollowUpByEnvironmentalHealthDepartment
-- WHEN ED.ID = 14 THEN FollowUpByFamilyCommunityServicesDepartment
-- WHEN ED.ID = 15 THEN FollowUpByFire
-- WHEN ED.ID = 16 THEN FollowUpByLegal
-- WHEN ED.ID = 17 THEN FollowUpByMayorOffice
-- WHEN ED.ID = 18 THEN FollowUpByMunicipalDevelopmentDepartment
-- WHEN ED.ID = 19 THEN FollowUpByParksRecreationDepartment
-- WHEN ED.ID = 20 THEN FollowUpByPlanningDepartment
-- WHEN ED.ID = 21 THEN FollowUpBySeniorAffairs
-- WHEN ED.ID = 22 THEN FollowUpBySolidWaste
-- WHEN ED.ID = 23 THEN FollowUpByTransit ELSE NULL END AS FollowUp_Desc
,ED.FollowUp_Desc
,NotificationSentDate
FROM [dbo].[TraCSCommunityEventSummary] tcsc -- Don't forget to create ID Column in 'TraCSCommunityEventSummary' table from the ctblEmailDetail table.
LEFT JOIN ctblEmailDetail WITH (NOLOCK) ED ON tcsc.ID = ED.ID
WHERE tcsc.RequiresOfficerFollowUp = '1'
OR tcsc.RequiresOtherFollowUp = '1'
AND tcsc. NotificationSentDate IS NULL
SELECT @EmailAddress = coalesce(@EmailAddress + ';', '') + EmailAddress
FROM [dimAgencyEmail] d
OUTER APPLY (
SELECT MAX(ID) as Id
FROM [dimAgencyEmail] d
GROUP BY ID
) c on c.Id = d.Id
SELECT @EmailAddress;
SELECT DISTINCT D.DepartmentName, d.EmailAddress
FROM [dimAgencyEmail] d
WHERE (d.DepartmentName In ('Animal Welfare', 'Aviation Department','City Council',
'Civilian Police Oversite Agency','Enviromental Health Dept.',
'Family & Community Services Dept.','Fire','Legal Depaartment',
'Mayors Office','Municipal Development Dept.','Parks & Recration Dept.',
'Planning Dept.','Senior Affairs Dept.','Sold Waste Dept.','Transit Dept.')) ;
SELECT tcsc.EventTitle
,tcsc.StartDate
,tcsc.IssuesIdentified
,tcsc.RequiresOfficerFollowUp
,tcsc.RequiresOtherFollowUp
,tcsc.NotificationSentDate
,CED.FollowUp_Desc
FROM [dbo].[TraCSCommunityEventSummary] tcsc
LEFT JOIN ctblEmailDetail CED ON tcsc.ID = CED.ID
WHERE tcsc.RequiresOfficerFollowUp = '1'
OR tcsc.RequiresOtherFollowUp = '1'
AND tcsc. NotificationSentDate IS NULL
-- I didnt get time to look after this line ;)
DECLARE c1 CURSOR FOR
SELECT EventTitle,StartDate,IssuesIdentified,RequiresOfficerFollowUp,RequiresOtherFollowUp, FollowUpByHorseMountedUnit,
FollowUpByArmedRobbery ,FollowUpByBurglary ,
FollowUpByAutoTheft ,FollowUpByNarcotics ,FollowUpByVice ,FollowUpByGangs ,
FollowUpByMotorUnit ,FollowUpByAnimalWelfare ,FollowUpByAviation ,
FollowUpByCityCouncil ,FollowUpByCivilianPoliceOversightAgency ,
FollowUpByEnvironmentalHealthDepartment ,FollowUpByFamilyCommunityServicesDepartment ,
FollowUpByFire ,FollowUpByLegal ,FollowUpByMayorOffice , FollowUpByMunicipalDevelopmentDepartment ,
FollowUpByParksRecreationDepartment ,FollowUpByPlanningDepartment ,FollowUpBySeniorAffairs ,
FollowUpBySolidWaste ,FollowUpByTransit,NotificationSentDate
FROM @EmailDetail
OPEN c1
FETCH NEXT FROM c1 INTO @EventTitle,@StartDate,@IssuesIdentified,@RequiresOfficerFollowUp,@RequiresOtherFollowUp,
@FollowUpByHorseMountedUnit,@FollowUpByArmedRobbery, @FollowUpByBurglary ,
@FollowUpByAutoTheft ,@FollowUpByNarcotics ,@FollowUpByVice ,@FollowUpByGangs ,
@FollowUpByMotorUnit ,@FollowUpByAnimalWelfare ,@FollowUpByAviation ,
@FollowUpByCityCouncil ,@FollowUpByCivilianPoliceOversightAgency ,
@FollowUpByEnvironmentalHealthDepartment ,@FollowUpByFamilyCommunityServicesDepartment ,
@FollowUpByFire ,@FollowUpByLegal ,@FollowUpByMayorOffice , @FollowUpByMunicipalDevelopmentDepartment ,
@FollowUpByParksRecreationDepartment ,@FollowUpByPlanningDepartment ,@FollowUpBySeniorAffairs ,
@FollowUpBySolidWaste ,@FollowUpByTransit,@NotificationSentDate
WHILE @@FETCH_STATUS <> -1
BEGIN
SET @Body = 'Sent to: ' + @EmailAddress + ';
To Whom It May concern:<br><br>
On '+ @StartDate +' an xxxx was at '+ @EventTitle +' and these issues were identified:<br>
'+ @IssuesIdentified +' and marked as requiring your assistance to follow up with the concern.<br>
For the xxx Center, please create a ticket for the xxxxx.<br>
'+ @DepartmentName +' there are items on this request that require follow up by their office.<br><br>
Thank you for your prompt attention to this matter.'
BEGIN
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'xxx',
@recipients = 'xxxxxx@xxxx.xxx', -- @EmailAddress,
@from_address = 'noreply@xxxx.xxx',
@reply_to = 'noreply@xxxx.xxx',
@subject = @subject,
@body = @body,
@body_format = 'HTML',
@importance = 'High',
@sensitivity = 'Confidential';
END
end