2013-08-20 29 views
-1

有沒有一種方法可以讓我在下面的查詢中添加時間,我的電子郵件正文:添加具有sp_send_dbmail電子郵件(數據庫郵件)的體內動態內容

EXEC msdb.dbo.sp_send_dbmail 
    @profile_name = 'TEST_DEV', 
    @recipients = '[email protected]', 
    @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()來代替xy和DATEADD(MINUTE,-150,GETDATE())來代替yy嗎?

是否有可能?

declare @body nvarchar(max) 
EXEC msdb.dbo.sp_send_dbmail 
    @profile_name = 'DEV', 
    @recipients = '[email protected]', 
    @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 ; 

你想讓我做這樣的事嗎?

回答

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 = '[email protected]', 
    @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; 
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 = '[email protected]', 
    @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

+0

'@ body'參數的類型應該是'NVARCHAR(something)'。 [鏈接](http://technet.microsoft.com/en-us/library/ms190307.aspx) –

+0

謝謝,更正。我正在看同一頁,並沒有看到'varchar'之前的'n' O.o –

+0

嘿tommy_0,我更新了我的答案?這是你的意思嗎 ?它不起作用,但我不確定我是否正確理解了你的觀點。你是指將@body變量傳遞給sp_send_dbmail。你能看看我編輯的答案並讓我知道嗎? – CodeNinja