我将从SQL Server表中导出数据到一个
我的问题在于这个值:
我尝试过这种方法,但是没有起作用: 注意:这个
.csv
文件,然后使用sp_send_email
将带有数据的文件发送电子邮件。我的问题在于这个值:
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 = '6lack@email.com',
@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;