Search code examples
sql-serversp-send-dbmail

Why do I get error -2147467259 when trying to send an email in SQL Server


I am trying to send an email containing the results of a stored procedure call inside SQL Server.

declare @querytext nvarchar(100)
set @querytext = 'sp_get_SPresults @item_name = ''Name of Group'''';
EXEC msdb.dbo.sp_send_dbmail  
    @profile_name = 'My Email',  
    @recipients = 'anaddress@adomain.com',  
    @body = 'Email',  
    @subject = 'Suitable Subject' ,     
    @query = @querytext,
    @attach_query_result_as_file = 1,
@query_result_header = 1,
@query_no_truncate = 1;

But when I run this, I get a

Msg 22050, Level 16, State 1, Line 23 Failed to initialize sqlcmd library with error number -2147467259 error.

When I send a test email in SQL Server it sends it fine. Even just setting @querytext as "select * from [Table]" doesn't work. I've seen elsewhere that including @query_result_header=1 can clear this kind of issue, but it doesn't appear to make a difference here.

Any help would be appreciated.

Thanks


Solution

  • In the end, for whatever reason, I tried using just the following parameters:

    • @profile_name
    • @recipients
    • @body
    • @subject
    • @body_format
    • @execute_query_database

    This now works. Thanks everyone for your help. :)