我正在尝试通过sp_send_dbmail发送csv文件,并且其中一个字段(结果)中包含逗号。运行以下代码时,它将自动将某些结果分成两列。
我尝试过更改@query_result_separator,但是除了结果字段(最终会给我两列)之外,它根本不会分隔任何内容。
declare @filename NVARCHAR(MAX)
DECLARE @prefix varchar(50)
DECLARE @q varchar(max)
DECLARE @body NVARCHAR(MAX)
SET @body = 'abc'
SET @filename = concat('abc_',replace(cast(cast(getdate() as date) as varchar),'-','_'),'.csv')
SET @prefix = '[sep=,' + CHAR(13) + CHAR(10) + 'ID]'
SET @q ='set nocount on; select right(''"=""'' + cast(ID as nvarchar) + ''"""'', 24)' + @prefix + ',Outcome, isnull(cast(Outcomedate as varchar),'''') as OutcomeDate,isnull(cast(Alerts as varchar),'''') as Alerts
from database.abc.defg'
EXEC msdb.dbo.sp_send_dbmail
@body = @body,
@body_format ='HTML',
@recipients = 'abc@defg.com',
@subject = 'abc',
@query = @q ,
@attach_query_result_as_file = 1,
@query_attachment_filename = @filename,
@query_result_separator = ',',
@query_result_no_padding = 1,
@query_result_width = 500;