2014-09-30 59 views
1

我有一個作業設置,當磁盤空間低於20%時發送電子郵件。現在,除了電子郵件中的結果之外,一切都很好。我試圖將query_result_width更改爲每種可能的變體。我想以純文本的形式發送它,因爲某些上層電子郵件出於某種原因只能用純文本。這些行看起來像是在其他行上生成的,並且結果如下,結果就是一個例子。SQL電子郵件行未對齊

ServerName           Drive 
          Free(MB) 

MyServerName          C 
          5,468 

應該像這樣:

ServerName    Free(MB)     Drive 

MyServerName    5,468      C 

下面是使用腳本,任何幫助將不勝感激。

execute msdb.dbo.sp_send_dbmail 
     @profile_name = 'ProfileName', 
     @recipients = '[email protected], 
     @subject = 'WARNING: Disk space in one or more drives is below 20 % free space', 
     @body_format = 'TEXT', 
     @Body = 'These are the servers and drives with low free space', 
     @query_result_width = 134, 
     @query_result_header = 0, 
     @query ='SET NOCOUNT ON; 
        SELECT "ServerName" as [ServerName], "Drive" as [Drive], "Total(MB)" as [Total(MB)], "Free(MB)" as [Free(MB)], "Used(MB)" as [Used(MB)], "Free(%)" as [Free(%)] 
        UNION ALL 
        SELECT [ServerName] 
        ,[Drive] 
        ,[Total(MB)] 
        ,[Free(MB)] 
        ,[Used(MB)] 
        ,[Free(%)] 
       FROM [DBA].[dbo].[ServerDriveSpace] 
       WHERE [Free(%)] < 20' 

回答

1

你爲什麼不只是將其添加爲附件而不是電子郵件中的文字。尺寸仍然相對較小,並且會帶來更好的外觀,這也解決了您的對齊問題。像這樣的東西....

DECLARE @tab char(1) = CHAR(9) 
execute msdb.dbo.sp_send_dbmail 
    @profile_name = 'ProfileName', 
    @recipients = '[email protected]', 
    @subject = 'WARNING: Disk space in one or more drives is below 20 % free space', 
    @body_format = 'TEXT', 
    @Body = 'These are the servers and drives with low free space', 
    @attach_query_result_as_file = 1, 
    @query_attachment_filename='DiskSpace.csv', 
    @query_result_width = 134, 
    @query_result_header = 0, 
    @[email protected], 
    @query_result_no_padding=1, 
    @query ='SET NOCOUNT ON; 
       SELECT "ServerName" as [ServerName], "Drive" as [Drive], "Total(MB)" as [Total(MB)], "Free(MB)" as [Free(MB)], "Used(MB)" as [Used(MB)], "Free(%)" as [Free(%)] 
       UNION ALL 
       SELECT [ServerName] 
       ,[Drive] 
       ,[Total(MB)] 
       ,[Free(MB)] 
       ,[Used(MB)] 
       ,[Free(%)] 
      FROM [DBA].[dbo].[ServerDriveSpace] 
      WHERE [Free(%)] < 20' 
0

DECLARE @NewLineChar AS CHAR(2)= CHAR(13)+ CHAR(10) 其應用(其主要是嘗試和錯誤)