Does anyone know if something changed between SQL 2012 and SQL 2019 to the sp_send_dbmail for the @query_no_truncate expectation? Is there some new condition that was implemented or now enforced which results in my error situation such as, if one of the other parameters is not true then the @query_no_truncate cannot be true or something?

A process I am using that calls sp_send_dbmail was copied directly from my previous instance of SQL Server 2012-where it has been running perfectly for years--into my new server SQL Server 2019. Since that migration to 2019 from 2012 the process will not work. I have been chasing this error for two days on and off now and tracked it down to setting the @query_no_truncate value = 1. Whenever it is set to 0 it works just fine but when set to 1, the process will error with message: "Failed to initialize sqlcmd library with error number -2147024809." and after adding some additional code, I get it to tell me the real problem which is: "The data type of substitution parameter 1 does not match the expected type of the format specification."

In reading the documentation on line that input parameter is expected to be a bit value so setting it to 1 is acceptable. I even tried to declare local variables for @true = 1 and @false = 0 as BIT and assign the values using those variables but it still complains if I set the @query_no_truncate to 1.

Within my code I gather information and set all the parameters so the end result is to just execute the query with those variables as shown below and it has worked for the past 10 years just fine:

        DECLARE @true BIT = 1, @false BIT = 0;
        exec @rtn = msdb.dbo.sp_send_dbmail 
            @profile_name=@profileName, 
            @recipients=@fromAddress,
            @copy_recipients=NULL,
            @blind_copy_recipients=@recipientsList,
            @subject=@emailSubject,
            @body_format = @bodyFormat,
            @importance= 'NORMAL',
            @sensitivity= 'NORMAL',
            @file_attachments=@file,  
            @query=@emailQuery,
            @execute_query_database= @dbName,  
            @attach_query_result_as_file = @false,
            @query_attachment_filename= @file,  
            @query_result_header = @false,
            @query_result_width=32767,            
            @query_result_separator='',
            @exclude_query_output = @false,
            @append_query_error = @false,
            @query_no_truncate = @false,
            @query_result_no_padding = @false,
            @mailitem_id = 0,
            @from_address=@fromAddress,
            @reply_to=@replyToAddress;

with the process setting the value as @query_no_truncate = 1; so,

0

There are 0 best solutions below