Failed to execute msdb.dbo.sp_send_dbmail

133 views Asked by At

I'm getting this error:

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

My code is:

IF EXISTS (SELECT * FROM [StagingMX].[dbo].[_CVA] 
           WHERE DATEDIFF(DAY, [Completion time], GETDATE()) = 1 
             AND [A] = 'IQQ' AND [B] = 'MDD')
BEGIN
    EXEC msdb.dbo.sp_send_dbmail 
              @profile_name = 'Admin', 
              @recipients = '[email protected]',
              @body = 'Good Morning,',
              @query = 'SELECT * FROM [StagingMX].[dbo].[_CVA] WHERE DATEDIFF(day, [Completion time], GETDATE()) = 1 and [A] = IQQ AND [B] = MDD;',
              @subject = 'Customer Voice';
END

The problem has to be something in relation with how I'm trying to implement the query in regards to the email context (what's inside the query works fine if I run it by itself outside of the email). I've been successful in sending other emails and implementing other queries.

For example, if I modify the email query to this one it works perfectly:

@query = 'SELECT TOP(1) * FROM [StagingMX].[dbo].[_CVA] ORDER  BY [ID] DESC;',

I've already done:

GRANT EXECUTE ON sp_send_dbmail TO PUBLIC
0

There are 0 answers