我正在将数据从SQL Server表导出到.csv
文件,然后使用sp_send_email
通过数据向文件发送电子邮件。SQL Server:在字符串中隐藏标点符号
我的问题是这个值:
Cantata Number 212 "Peasants Cantata", BWV 212
值会分成两列在.csv
文件被通过电子邮件发送。该值应该只在一列中。
某些标题可能包含逗号,这些逗号需要保留在字符串中以供这些实例使用。
例如:
Cantata Number 212 Peasants Cantata" BWV 212"
这个方法我试过,但不工作:
注意:此SELECT
声明驻留在视图内vw_WeeklyReport
SELECT TOP 100 PERCENT
'"' + [p].[Title] + '"' [Title]
FROM
table
的代码导出数据并通过电子邮件发送.csv
文件:
BEGIN
SET NOCOUNT ON;
DECLARE @qry VARCHAR(8000);
-- Create the query, concatenating the column name as an alias
SET @Qry = 'SET NOCOUNT ON; SELECT Title FROM [vw_WeeklyReport] SET NOCOUNT OFF';
-- Send the e-mail with the query results in attachment.
EXEC [msdb].[dbo].[sp_send_dbmail]
@profile_name = 'default',
@recipients = '[email protected]',
@subject = 'Weekly Report',
@body = 'An attachment has been included in this email.',
@query_attachment_filename = 'WeeklyRep.csv',
@query = @qry,
@attach_query_result_as_file = 1,
@query_result_separator = ',',
@query_result_width = 32767,
@query_result_no_padding = 1;
END;
如果删除逗号是可以接受的,则应该考虑使用SQL中的'replace'来完成该操作。 – user2366842
某些值将包含逗号,并且必须将逗号留在字符串中。 –
另一种选择是重做导出以使用除逗号以外的其他分隔符,尽管这可能不在单个SO问题的范围之内。 – user2366842