无法从 EXEC msdb.dbo.sp_send_dbmail 获取我的电子邮件文件中的标头

问题描述 投票:0回答:1

此 sql 生成我在 Excel 文件中寻找的正确格式,但一旦通过电子邮件发送,它不会向我的 Excel 文件添加标头。我玩了一些设置,然后它抛出错误-2147467259。我尝试过使用 @query_result_header = 1 和 @query_result_header = 0。我被这个难住了。

set @SQL2 = 
'


SELECT [Rx_Date]
      ,[Fill_Date]
      ,convert(varchar(10), [Days_Supply]) 
      ,convert(varchar(10), [Quantity_Dispensed]) 
      ,convert(varchar(11),[NDC]) 
      ,[Drug_Name]
      ,[Pay_Type]
      ,[Prescriber_DEA_Nbr]
      ,convert(varchar(10),[Prescriber_NPI_Nbr]) 
      ,[Prescriber_Last_Name]
      ,[Prescriber_First_Name]
      ,[Prescriber_Middle_Name]
      ,[Prescriber_Address_1]
      ,[Prescriber_Address_2]
      ,[Prescriber_City]
      ,[Prescriber_State]
      ,convert(varchar(5),[Prescriber_Zip]) 
      ,convert(varchar(15),[Patient_ID]) 
      ,convert(varchar(5),[Patient_Zip]) 
  FROM [Integrity].[dbo].[ab_consumption_review_results]'




DECLARE @sbj as varchar(200) = 'AB Consumption Review '+ CAST(CAST(GETDATE() as date) as varchar(12))
DECLARE @fileName AS VARCHAR(20)
DECLARE @body as varchar (200)


SET @fileName =  @str + '_' + (SELECT [PHRM_DEA_NBR] FROM [Reference].[dbo].[Stores] where [STORE_NBR] = @str
   and STORE_OPEN_STATUS_IND = 'y')  + '.xls'

SET @body ='Hello, 
<br/> Attached is the three month AB Consumption Review '+ @start  + ' to '+ @end +'.'

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Integrity',
@subject = @sbj,
@body = @body,
@body_format ='HTML',
@recipients = '[email protected]',
@query = @SQL2,
@execute_query_database = 'Integrity',
@attach_query_result_as_file = 1,
@query_result_header  = 1,
@query_no_truncate = 1,
@query_result_no_padding = 0,
@query_result_separator =  '    ',
@query_attachment_filename = @fileName

我也为我的@SQL2尝试过这个,但没有运气


set @SQL2 = 
'
SET NOCOUNT ON
SELECT ''Rx_Date'' as Rx_Date
      ,''Fill_Date'' as Fill_Date
      ,''Days_Supply'' as Days_Supply
      ,''Quantity_Dispensed'' as Quantity_Dispensed
      ,''NDC'' as NDC
      ,''Drug_Name'' as Drug_Name
      ,''Pay_Type'' as Pay_Type
      ,''Prescriber_DEA_Nbr'' as Prescriber_DEA_Nbr
      ,''Prescriber_NPI_Nbr'' as Prescriber_NPI_Nbr
      ,''Prescriber_Last_Name'' as Prescriber_Last_Name
      ,''Prescriber_First_Name'' as Prescriber_First_Name
      ,''Prescriber_Middle_Name'' as Prescriber_Middle_Name
      ,''Prescriber_Address_1'' as Prescriber_Address_1
      ,''Prescriber_Address_2'' as Prescriber_Address_2
      ,''Prescriber_City'' as Prescriber_City
      ,''Prescriber_State'' as Prescriber_State
      ,''Prescriber_Zip'' as Prescriber_Zip
      ,''Patient_ID'' as Patient_ID
      ,''Patient_Zip'' as Patient_Zip

      UNION ALL




SELECT [Rx_Date]
      ,[Fill_Date]
      ,convert(varchar(10), [Days_Supply]) 
      ,convert(varchar(10), [Quantity_Dispensed]) 
      ,convert(varchar(11),[NDC]) 
      ,[Drug_Name]
      ,[Pay_Type]
      ,[Prescriber_DEA_Nbr]
      ,convert(varchar(10),[Prescriber_NPI_Nbr]) 
      ,[Prescriber_Last_Name]
      ,[Prescriber_First_Name]
      ,[Prescriber_Middle_Name]
      ,[Prescriber_Address_1]
      ,[Prescriber_Address_2]
      ,[Prescriber_City]
      ,[Prescriber_State]
      ,convert(varchar(5),[Prescriber_Zip]) 
      ,convert(varchar(15),[Patient_ID]) 
      ,convert(varchar(5),[Patient_Zip]) 
  FROM [Integrity].[dbo].[ab_consumption_review_results]'
sql-server stored-procedures header
1个回答
1
投票

我也遇到了同样的问题。我所做的解决方法是将标头添加到选择查询并将其与真正的选择查询合并:

  SELECT '0' as 'Row Count'

      ,'[Rx_Date]'
      ,'[Fill_Date]'
      ,'[Days_Supply]' 
      ,'[Quantity_Dispensed]' 
      ,'[NDC]' 
      ,'[Drug_Name]'
      ,'[Pay_Type]'
      ,'[Prescriber_DEA_Nbr]'
      ,'[Prescriber_NPI_Nbr]' 
      ,'[Prescriber_Last_Name]'
      ,'[Prescriber_First_Name]'
      ,'[Prescriber_Middle_Name]'
      ,'[Prescriber_Address_1]'
      ,'[Prescriber_Address_2]'
      ,'[Prescriber_City]'
      ,'[Prescriber_State]'
      ,'[Prescriber_Zip]' 
      ,'[Patient_ID]' 
      ,'[Patient_Zip]' 
UNION
SELECT 
      '1' as 'RowCount'
      ,[Rx_Date]
      ,[Fill_Date]
      ,convert(varchar(10), [Days_Supply]) 
      ,convert(varchar(10), [Quantity_Dispensed]) 
      ,convert(varchar(11),[NDC]) 
      ,[Drug_Name]
      ,[Pay_Type]
      ,[Prescriber_DEA_Nbr]
      ,convert(varchar(10),[Prescriber_NPI_Nbr]) 
      ,[Prescriber_Last_Name]
      ,[Prescriber_First_Name]
      ,[Prescriber_Middle_Name]
      ,[Prescriber_Address_1]
      ,[Prescriber_Address_2]
      ,[Prescriber_City]
      ,[Prescriber_State]
      ,convert(varchar(5),[Prescriber_Zip]) 
      ,convert(varchar(15),[Patient_ID]) 
      ,convert(varchar(5),[Patient_Zip]) 
  FROM [Integrity].[dbo].[ab_consumption_review_results]
  'ORDER BY 'Row Count'

还在选择查询中添加了“行计数”,以将标题保持在查询结果的顶部。

希望有帮助! :)

© www.soinside.com 2019 - 2024. All rights reserved.