I am using sp_send_dbmail to send an email containing entries in table Persons. It throws following error -
Failed to initialize sqlcmd library with error number -2147467259.
My query is below -
declare #q varchar(max)
SET #q='SELECT FirstName,LastName FROM dw_extract.dbo.Persons'
USE msdb
EXEC sp_send_dbmail
#profile_name = 'profile_name',
#recipients = 'abc#xyz.com',
#subject = 'T-SQL Query Result',
#body = 'The result from SELECT is appended below',
#execute_query_database = 'dw_extract',
#query = #q
I did some research and found that few people who faced such problem could get rid of it by explicitly stating column names and database of the table used. I did that but still it continues to throw the error.
Related
Using SQL-Server 2019; I am trying to use sp_send_dbmail and have successfully sent test emails from SQL-Server so believe the configuration is fine.
A sproc runs in the execution context of a SQL-Login, which updates some tables in a database and then calls the sproc below which is supposed to send emails via msdb.dbo.sp_send_dbmail.
CREATE PROCEDURE [dbo].[sp_SendEmail]
#emailAddress AS NVARCHAR(MAX),
#emailBody AS NVARCHAR(MAX),
#emailSubject AS NVARCHAR(150),
#attachmentPath AS NVARCHAR(255) = NULL,
#emailId AS INT OUTPUT
WITH EXECUTE AS N'Bob'
AS
BEGIN
SET NOCOUNT ON;
DECLARE #bSuccess AS INT = 0;
EXECUTE #bSuccess = [msdb].[dbo].[sp_send_dbmail]
#profile_name = N'Profile',
#recipients = #emailAddress,
#subject = #emailSubject,
#body = #emailBody,
#body_format = N'HTML',
#importance = N'High',
#file_attachments = #attachmentPath,
#mailitem_id = #emailId OUTPUT;
RETURN #bSuccess;
END
GO
When I run the sproc I get the error:
Msg 229, Level 14, State 5, Procedure msdb.dbo.sp_send_dbmail, Line 1
[Batch Start Line 2] The EXECUTE permission was denied on the object
'sp_send_dbmail', database 'msdb', schema 'dbo'.
The SQL-Login, Bob, has a user mapping to the msdb database using the default schema and is a member of the DatabaseMailUserRole role.
I've given Bob execute permission on the sp_send_dbmail securable, which seems like a duplication of what the DatabaseMailUserRole provides, but still get the error above.
I'd appreciate any insight you can throw at me.
This question already has an answer here:
Sending attachment with sp_send_dbmail getting error Failed to initialize sqlcmd library with error number -2147467259
(1 answer)
Closed 1 year ago.
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
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. :)
I'm working on a piece of code that will email me the results of some queries twice a day from SQL Server 2016. When I run this bit of code from my developer window, it works fine. When I run it from the job, I get this error:
Executed as user: myDB\svcAGCRM2016_PRD. Failed to initialize sqlcmd
library with error number -2147467259. [SQLSTATE 42000] (Error 22050).
The step failed.
This is the code that runs:
declare #recipient_name varchar(500)
declare #SqlQuery varchar(max)
SET #SqlQuery='exec [myDB].[dbo].[sp_DailyRejectionRate]'
set #recipient_name = 'myemail#email.com'
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'SMTPProfile',
#recipients =#recipient_name,
#subject = '[INFO]'
,#query='exec [myDB].[dbo].[sp_DailyRejectionRate];
I'm running this from a service account. Weirdly, not with the name of the service account in the error. Actually, I can't even find that account.
In the code, I'm doing an exec because otherwise the query is too long for query.
The service account that runs this is an SA. Also, I wrote another query this morning that sends out email with not problems today.
Any ideas?
What if you explicitly specify the user in the query string?
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'SMTPProfile',
#recipients =#recipient_name,
#subject = '[INFO]'
,#query='execute as user='my_sa_user' exec [myDB].[dbo].[sp_DailyRejectionRate];
I'm using the sp_send_dbmail stored procedure, and I'm trying to include the result from a query in the body string. I can't seem to get this to work. Any ideas would be much appreciated.
use msdb
go
exec sp_send_dbmail
#profile_name = 'WarehouseEmailer',
#recipients = 'someone#example.com',
#subject = 'Database Mail Test 1',
#Body = 'This is a test email from SQL Server. <br> This should be line 2. <br> The
subject\'s first name is: '+ (select top(1) FirstName from Warehouse.dbo.Subject),
#body_format = 'HTML'
Obviously this example just a simple test, but it is exactly the type of data I'd like to pass to the stored procedure to include in the email body. I look forward to your responses and many thanks for any advice!
Dynamic SQL, or concatenated strings, cannot be passed into the procedures parameters like that.
Try this:
use msdb
go
declare #msgbody nvarchar(1000)
set #msgbody = 'This is a test email from SQL Server.' + char(10) + 'This should be line 2. <br> The
subject''s first name is: '+ (select top(1) FirstName from Warehouse.dbo.Subject)
exec sp_send_dbmail
#profile_name = 'WarehouseEmailer',
#recipients = 'someone#example.com',
#subject = 'Database Mail Test 1',
#Body = #msgbody,
#body_format = 'HTML'
I'm trying to use sp_send_dbmail to send the results of a query through a SQLAgent job in SQL Server 2014. I believe I have my DBMail profile set up properly but when running this:
exec msdb.dbo.sp_send_dbmail
#profile = 'TestProfile',
#recipients = 'testmail#gmail.com',
#subject = 'Test',
#query = 'SELECT id FROM TestTable',
#attach_query_result_as_file = 1,
#query_attachment_filename = 'TestValues.txt'
I get the following error message:
Failed to initialize sqlcmd library with error number -2147467259.
Googling this error message didn't turn up anything useful, likely due to the generic error number. Anyone have some insight into this error message?
I found that despite both my query window (for testing) and SqlAgent job were pointing at my desired DB, sp_send_dbmail doesn't seem to have any database context. My original post was failing because SQL didn't know where to run SELECT * FROM TestTable. The fix is to provide sp_send_dbmail with database context by either fully qualifying your table in the #query parameter:
#query = 'SELECT id FROM testDB.dbo.TestTable'
or by providing the optional #execute_query_database parameter:
#execute_query_database = 'testDB'
Enable sysadmin server role for the account that is used to run the SQL Server Agent.Below are the screenshots.
Error
Fix
Now the SQL Server Job runs without any errors and I get an email from dbmail.
There's another reason why you might get this error; if the query has an issue.
In our case we had (note that it's not due to a syntax error; note the missing quotes):
DECLARE #EmailQuery varchar(max) = 'select
e.Field1,
REPLACE(REPLACE(e.ExceptionReason, CHAR(13), ''), CHAR(10), '') ExceptionReason,
e.UserName
from dbo.tblException e'
Once we corrected it as follows, it worked fine:
DECLARE #EmailQuery varchar(max) = 'select
e.Field1,
REPLACE(REPLACE(e.ExceptionReason, CHAR(13), ''''), CHAR(10), '''') ExceptionReason,
e.UserName
from dbo.tblException e'