SQL电子邮件行未对齐

时间:2014-09-30 15:42:03

标签: sql sql-server sqlmail

我有一个作业设置,当磁盘空间低于20%时发送电子邮件。现在一切都很好,除非涉及到电子邮件中的结果。我尝试将query_result_width更改为每个可能的变体。我想以纯文本形式发送它,因为某些高层因为某些原因而只是像普通文本那样收到电子邮件。行看起来像是在每一行上生成的,结果如下所示。

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 = 'Emailaddress@email.com,
        @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'

2 个答案:

答案 0 :(得分:1)

为什么不将它作为附件而不是电子邮件中的文本添加。尺寸仍然相对较小,它会提供更好的外观,这也解决了你的对齐问题。像这样......

DECLARE @tab char(1) = CHAR(9)
execute  msdb.dbo.sp_send_dbmail
    @profile_name = 'ProfileName',
    @recipients = 'Emailaddress@email.com',
    @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,
    @query_result_separator=@tab,
    @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'

答案 1 :(得分:0)

DECLARE @NewLineChar AS CHAR(2)= CHAR(13)+ CHAR(10) 应用它(主要是尝试和错误)