将结果作为csv文件发送

时间:2017-07-19 20:32:51

标签: sql sql-server csv

我正在尝试让SQL Server Agent将其作为电子邮件发送

我得到的只是错误。它没有@query参数,但我没有看到问题。

EXEC msdb.dbo.sp_send_dbmail
     @profile_name = 'e-automate mail',
     @recipients = 'jbalter@seamlesssolutions.com',
     @subject = 'on hold customers',
     @body='Customers on Hold in E-automate',
     @query = 'SELECT CC.CustomerID AS 'E-auto Number',
                      CC.TextVal AS 'Autotask Number',
                      ARC.CustomerName
                  FROM [CoCopeco].[dbo].[ARCustomerCustomProperties] CC
                  INNER JOIN [CoCopeco].[dbo].[ARCustomers] ARC  ON ARC.CustomerID = CC.CustomerID
                  WHERE ShAttributeID = '2043' 
                    AND ARC.OnHoldCodeID IS NOT NULL
                    AND DATALENGTH (CC.TextVal) > 0 ' ,
    @attach_query_result_as_file=1,
    @query_attachment_filename = 'Results.csv',
    @query_result_separator = ','

1 个答案:

答案 0 :(得分:1)

您需要在@Query参数选项卡中使用其他单引号替换单引号,即替换'用''。

将@Query值放在下面 -

@query = 'SELECT CC.CustomerID AS ''E-auto Number'',
              CC.TextVal AS ''Autotask Number'',
              ARC.CustomerName
          FROM [CoCopeco].[dbo].[ARCustomerCustomProperties] CC
          INNER JOIN [CoCopeco].[dbo].[ARCustomers] ARC  ON ARC.CustomerID = CC.CustomerID
          WHERE ShAttributeID = ''2043'' 
            AND ARC.OnHoldCodeID IS NOT NULL
            AND DATALENGTH (CC.TextVal) > 0 '