有没有办法可以在以下查询中添加时间作为我的电子邮件正文:
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'TEST_DEV',
@recipients = 'xxx@gmail.com',
@query = ' select
Percentage = CONVERT(DECIMAL(10,1),100 - (CAST(COUNT(DISTINCT case when PD.Exception != ' ' then PD.Id END) as float)/CAST(COUNT(PD.Id) as float)*100))
from
DataBaseName.dbo.Product P INNER JOIN DataBaseName.dbo.LogProduct PD
ON P.LogId = PD.LogId
WHERE
ResponseTime < GETDATE() and RequestTime > DATEADD(MINUTE, -150, GETDATE())
' ,
@subject = 'Test',
@body = 'Please check the attached file for Providers with Many unsuccessful calls between the time xx an yy',
@attach_query_result_as_file = 1 ;
在当前行
@body = 'Please check the attached file for info on calls between the time xx an yy',
我想添加GetDate()代替xx和DATEADD(MINUTE,-150,GETDATE())代替yy?
有可能吗?
declare @body nvarchar(max)
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'DEV',
@recipients = 'xxx@gmail.com',
@query = 'exec Database.dbo.spTest' ,
@subject = 'Test',
select @body = 'Please check the attached file for info on calls between the time ........................',
@attach_query_result_as_file = 1 ;
你想让我这样做吗?
答案 0 :(得分:1)
您可以创建一个存储过程来发送此电子邮件[s]:
CREATE PROCEDURE MySchema.MyProcedure
(
@arg1 type1, ...
)
AS
BEGIN
DECLARE @style TINYINT;
SET @style = 108 -- hh:mm:ss ; For other style please visit http://technet.microsoft.com/en-us/library/ms187928.aspx
DECLARE @body_content NVARCHAR(MAX);
SET @body_content = 'Please check the attached file for Providers with Many unsuccessful calls between the time ' + CONVERT(VARCHAR(25),GETDATE(),@style) + ' and ' + CONVERT(VARCHAR(25),DATEADD(MINUTE, -150, GETDATE()),@style) ;
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'TEST_DEV',
@recipients = 'xxx@gmail.com',
@query = ' select
Percentage = CONVERT(DECIMAL(10,1),100 - (CAST(COUNT(DISTINCT case when PD.Exception != '' '' then PD.Id END) as float)/CAST(COUNT(PD.Id) as float)*100))
from
DataBaseName.dbo.Product P INNER JOIN DataBaseName.dbo.LogProduct PD
ON P.LogId = PD.LogId
WHERE
ResponseTime < GETDATE() and RequestTime > DATEADD(MINUTE, -150, GETDATE())
' ,
@subject = 'Test',
@body = @body_content,
@attach_query_result_as_file = 1 ;
END;
答案 1 :(得分:0)
您可以在EXEC语句之前声明@body变量,并使其成为您想要的任何字符串。
修改强>:
我将此更新为更详细。我没有sp_send_dbmail在任何地方进行测试,但我认为它应该可以正常工作。我创建了一个名为@bodyMsg的字符串变量,在存储过程调用之前将其设置为您想要的字符串,然后将值传递给sp_send_dbmail中的@body变量。
declare @bodyMsg nvarchar(max)
select @bodyMsg = 'Please check the attached file for info on calls between the time ' + convert(varchar,GETDATE()) + ' and ' + convert(varchar,DATEADD(mm, -150, getdate())) + '.'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'TEST_DEV',
@recipients = 'xxx@gmail.com',
@query = ' select
Percentage = CONVERT(DECIMAL(10,1),100 - (CAST(COUNT(DISTINCT case when PD.Exception != ' ' then PD.Id END) as float)/CAST(COUNT(PD.Id) as float)*100))
from
DataBaseName.dbo.Product P INNER JOIN DataBaseName.dbo.LogProduct PD
ON P.LogId = PD.LogId
WHERE
ResponseTime < GETDATE() and RequestTime > DATEADD(MINUTE, -150, GETDATE())
' ,
@subject = 'Test',
@body = @bodyMsg,
@attach_query_result_as_file = 1 ;
然后将@body变量传递给sp_send_dbmail存储过程。可以在此处找到不同的日期时间格式:http://technet.microsoft.com/en-us/library/ms187928.aspx