2017-07-19 60 views
1

我想讓SQL Server代理將此郵件作爲電子郵件發送發送結果爲csv文件

我所得到的只是錯誤。它沒有@query參數,但我沒有看到問題。

EXEC msdb.dbo.sp_send_dbmail 
    @profile_name = 'e-automate mail', 
    @recipients = '[email protected]', 
    @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

你得到哪些錯誤?除此之外:我不明白你給這個問題的標題的含義 – Marged

回答

1

您需要在@Query參數選項卡中用單引號替換單引號,即替換'with'。

把@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 ' 
+0

謝謝。就是這樣。 – jbalter

+0

很高興能有所幫助。請將其標記爲已回答,以便其他問題可以適用。 – VKarthik

+0

如何將其標記爲已回答? – jbalter