我有一个存储过程,它查看一个表以查找具有特定条件的新条目,并在找到时调用 SQL 邮件。一个作业每 15 分钟执行一次 sp。有时它会通过电子邮件发送新条目,但有时 SQL 作业会成功运行,但不会生成电子邮件——尽管我在 SSMS 中运行查询并在那 15 分钟范围内在表中找到新条目。我在调整/扩大时间参数后手动运行了该过程,它成功生成了计划作业运行时最初未发送的电子邮件,但报告运行成功。
我对来自其他表的通知使用了类似的代码,它们似乎始终如一地生成电子邮件,但是另一个查看同一个表的不同标准的 sp 也间歇性地生成电子邮件。
我通过将所有参数移动到 WHERE 语句而不是对其中一些参数使用 GROUP BY 和 HAVING 来调整我的代码。我还将时间范围向后移动了 15 分钟(<=31 和 >=15),或者在没有解决方案的情况下删除了最小时间参数。
我不确定问题出在我的代码、执行 sp 的作业、SQL 邮件,还是我正在查询的表(我使用的日期列是 datetime 数据类型)、SQL 版本、补丁级别等。任何想法从哪里开始寻找为什么它有时而不是其他人调用邮件?
ANSI_NULLS 和 QUOTED_IDENTIFIER 为 ON
BEGIN
set nocount on
if EXISTS
(select v.vst_ext_id
from ptdata pd (nolock)
join fndmst fnd (nolock) on pd.find_code = fnd.find_code
join VISIT v (nolock) on pd.vst_int_id = v.vst_int_id
where grp_code in ('BRST500')
and category_code in (select CAT_CODE from COLUMN_LIST where CAT_CODE like 'REST%')
and v.dschrg_ts is null
and DATEDIFF(MINUTE,pd.entered_date,getdate()) <= 16
and DATEDIFF(MINUTE,pd.entered_date,getdate()) >= 0
)
begin
execute msdb.dbo.sp_send_dbmail
@profile_name = 'profile',
@query_result_header = 0,
@recipients = 'email@domain.com',
@subject = 'Charting Alert',
@body = 'New Charting for ',
@execute_query_database = 'database',
@query ='set nocount on
(select v.vst_ext_id
+ SPACE(5)
+ "Location: "
+ bd.loc_ds
+ SPACE(5)
+ "Entered for Date: "
+ convert(varchar(10),entered_for_date, 101) + " " + CONVERT(varchar(5),entered_for_date, 108)
+ SPACE(5)
+ "Charted: "
+ fnd.find_description
from t_ptdata pd (nolock)
join t_fndmst fnd (nolock) on pd.find_code = fnd.find_code
join TPM300_PAT_VISIT v (nolock) on pd.vst_int_id = v.vst_int_id
join TSM950_LOCATION_REF bd (nolock) on v.loc_lvl_5_id = bd.loc_int_id
where grp_code in ("BRST500")
and category_code in (select CAT_CODE from t_COLUMN_LIST where CAT_CODE like "REST%")
and v.dschrg_ts is null
and DATEDIFF(MINUTE,pd.entered_date,getdate()) <= 16
and DATEDIFF(MINUTE,pd.entered_date,getdate()) >= 0 )'
END
END
GO
这是一个在添加新记录时似乎始终如一地生成电子邮件的示例(实际上在同一个存储过程中):
--================================
--Begin Orders
--================================
BEGIN
set nocount on
if EXISTS
(select v.vst_ext_id
from ORDER o (nolock)
join ORDER_DETAIL od (nolock) on o.ord_int_id = od.ord_int_id
left join ORDER_CODE oc (nolock) on od.order_code_int_id = oc.order_code_int_id
left join VISIT v (nolock) on o.vst_int_id = v.vst_int_id
join LOCATION_REF bd (nolock) on v.loc_lvl_5_id = bd.loc_int_id
where od.order_code_int_id in (select order_code_int_id from ORDER_CODE (nolock) where upper(order_code_desc1) like '%RESTRA%')
and v.dschrg_ts is null
and DATEDIFF(MINUTE,o.entered_datetime_ts,getdate()) <= 16
and DATEDIFF(MINUTE,o.entered_datetime_ts,getdate()) >= 0
)
begin
execute msdb.dbo.sp_send_dbmail
@profile_name = 'profile',
@query_result_header = 0,
@recipients = 'email@domain.com',
@subject = 'Order Alert',
@body = 'New Order for ',
@execute_query_database = 'database',
@query ='set nocount on
(select v.vst_ext_id
+ SPACE(5)
+ "Location: "
+ bd.loc_ds
+ SPACE(5)
+ "Ordered for Date: "
+ convert(varchar(10),o.start_datetime_ts, 101) + " " + CONVERT(varchar(5),o.start_datetime_ts, 108)
+ SPACE(5)
+ "Order: "
+ oc.order_code_desc1
from ORDER o (nolock)
join ORDER_DETAIL od (nolock) on o.ord_int_id = od.ord_int_id
left join ORDER_CODE oc (nolock) on od.order_code_int_id = oc.order_code_int_id
left join VISIT v (nolock) on o.vst_int_id = v.vst_int_id
join LOCATION bd (nolock) on v.loc_lvl_5_id = bd.loc_int_id
where od.order_code_int_id in (select order_code_int_id from ORDER_CODE (nolock) where upper(order_code_desc1) like "%RESTRA%")
and v.dschrg_ts is null
and DATEDIFF(MINUTE,o.entered_datetime_ts,getdate()) <= 16
and DATEDIFF(MINUTE,o.entered_datetime_ts,getdate()) >= 0
)'
END
END
GO
这是另一个代码,它也间歇性地生成电子邮件,但使用相同的主表 - 没有所有连接:
Begin
set nocount on
if EXISTS
(select visit_id
from ptdata
where grp_code = 'NUSC1' and find_code = 'K_TOTAL'
and
DATEDIFF(MINUTE,entered_date,GETDATE()) < 16
and value > 1
)
begin
execute msdb.dbo.sp_send_dbmail
@profile_name = 'profile',
@query_result_header = 0,
@recipients = 'email@domain.com',
@subject = 'Screen Score',
@body = 'Screening Score above 1 for ',
@execute_query_database = 'database',
@query ='set nocount on
(select rtrim(visit_id) + space(5) + "Value: " + rtrim(value)
from ptdata
where grp_code = "NUSC1" and find_code = "K_TOTAL"
and
DATEDIFF(MINUTE,entered_date,getdate()) < 16
and value > 1
)'
END
END
GO