patternMinor
Is there a way to force sp_send_dbmail to use ANSI, or to not include the Unicode prefix?
Viewed 0 times
theforceincludewayunicodeansiprefixsp_send_dbmailtherenot
Problem
This is pretty trivial, but if someone has the answer handy, I'd appreciate it.
I use the
There are a number of workarounds: use a real reporting platform (we have Crystal, but it's never worked reliably); send the file to a little app that converts the file to ANSI and sends it on; use tab delimiters (my favorite); or educate the recipients about how to work with CSVs (just kidding, we all know that's not possible).
However, I'd love to just flip a switch somewhere and tell SQL Server to stop creating the attachments as Unicode, as I have no such need. I haven't found such a setting yet; is this possible?
Thanks!
EDIT
Based on Rohan's answer, this script in a SQL Agent job step almost works:
The file now has a "#TYPE System.Data.DataRow" prefix, instead of 0xFF FE, but it's now ANSI, so that's something.
I use the
msdb..sp_send_dbmail stored procedure as a lightweight way to distribute simple reports. This often entails producing a CSV. However, the stored procedure includes the Unicode little-endian prefix FF FE. This confuses Excel, causing it to not automatically parse the CSV into columns.There are a number of workarounds: use a real reporting platform (we have Crystal, but it's never worked reliably); send the file to a little app that converts the file to ANSI and sends it on; use tab delimiters (my favorite); or educate the recipients about how to work with CSVs (just kidding, we all know that's not possible).
However, I'd love to just flip a switch somewhere and tell SQL Server to stop creating the attachments as Unicode, as I have no such need. I haven't found such a setting yet; is this possible?
Thanks!
EDIT
Based on Rohan's answer, this script in a SQL Agent job step almost works:
$reportQuery = "EXEC Routine..TopTenReviewsReport @DoNotLog = 1"
$filePath = "\\SQLBI001\RawData\TopTenReviewsReport.csv"
$mailQuery = "
EXEC msdb..sp_send_dbmail
@profile_name = 'X',
@recipients = 'X',
@subject = 'X',
@file_attachments = '" + $filePath + "'"
Invoke-Sqlcmd -query $reportQuery | export-csv $filePath
$result = Invoke-Sqlcmd -query $mailQueryThe file now has a "#TYPE System.Data.DataRow" prefix, instead of 0xFF FE, but it's now ANSI, so that's something.
Solution
I think you can generate the reports using powershell and then use DBMail to mail the attachments
Source: Link
Write-host "Running SQL Query to export to ANSI CSV file"
Invoke-Sqlcmd -Query $dbQuery -ServerInstance $instanceName | export-csv $targetFileAnsi
Write-host "Running SQL Query to export to Unicode CSV file"
Invoke-Sqlcmd -Query $dbQuery -ServerInstance $instanceName | export-csv $targetFileUnicode -encoding "unicode"
$dbMailQuery1 = "execute msdb..sp_send_dbmail
@profile_name = 'msft_profile',
@recipients = 'seths@microsoft.com',
@subject = 'Email with Attachment - ANSI attachment',
@body_format = 'TEXT',
@body = 'This email has ANSI file attached to it',
@file_attachments = 'c:\temp\result_ansi.csv' "Source: Link
Code Snippets
Write-host "Running SQL Query to export to ANSI CSV file"
Invoke-Sqlcmd -Query $dbQuery -ServerInstance $instanceName | export-csv $targetFileAnsi
Write-host "Running SQL Query to export to Unicode CSV file"
Invoke-Sqlcmd -Query $dbQuery -ServerInstance $instanceName | export-csv $targetFileUnicode -encoding "unicode"
$dbMailQuery1 = "execute msdb..sp_send_dbmail
@profile_name = 'msft_profile',
@recipients = 'seths@microsoft.com',
@subject = 'Email with Attachment - ANSI attachment',
@body_format = 'TEXT',
@body = 'This email has ANSI file attached to it',
@file_attachments = 'c:\temp\result_ansi.csv' "Context
StackExchange Database Administrators Q#6290, answer score: 2
Revisions (0)
No revisions yet.