SQL Server: DB Mail Not getting triggered after certain Size - sql

I am sending csv file from SQL Server using below query.
EXEC #RtnCode = sp_send_dbmail
#profile_name = 'Mail',
#query_result_separator=#Separateur,
#recipients = 'xxx#xxx.com;',
#subject = #mailSubject,
#query = #STMT,
#Attach_Query_result_as_file = 1,
#query_attachment_filename = 'filename.csv',
#query_result_header = 1,
#query_result_width = 32767,
#query_result_no_padding = 1
Mail size in 'Database Mail' was configured as 1 MB. I have changed it to 5 MB. And restarted SQL Server agent. But Mails are not getting triggered, even though it is getting queued in Mail Queue.
Please help,
Thanks in advance.

Related

CSV file from the SQL Server is out of format

I am new to SQL Server and trying to send a CSV file as an email attachment to business users. Unfortunately, the file received is totally out of format. I have the following code and the table is from SAP B1. What would be the best way to resolve this?
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'profile name',
#recipients = 'recipent name',
#subject = 'Test',
#query = 'select slpcode, slpname, memo, commission from dbo.OSLP ',
#attach_query_result_as_file = 1,
#query_attachment_filename = 'Results.csv',
#query_result_separator = ',',
#query_result_width = 32767,
#query_result_header = 1,
#append_query_error = 1,
#query_no_truncate = 0
edit: Ideally, the data should be shown in only 4 columns as I have queried only 4 columns but instead this information is spread across columns A:N
Any help would be greatly appreciated.
The sample data in the table looks as follows:
Sample Data Link
and the result from my code shows up as follows:
Final Result Link
Thanks,

Send an email with CSV attachemt in SQL Server(SQL Job is failing)

When we run below script manually we are getting an email with attachment but when we are running through SQL Job getting below error.
Executed as user:venu: Failed to initialize the sqlcmd library with
error number -2147467259. [SQLSTATE 42000] (Error 22050). The step
failed.
/* To send an Email Attachment to the User */
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'DBMail',
#recipients = 'venu.siripuram#gamil.com',
#query = 'SET NOCOUNT ON
Select Recordtype as TransType, SKU, SKU1 Style, SKU2 Color,
SKU3 Size, UPC
from CIMSDE.dbo.ExportTransactions with (nolock)',
#subject = 'SKU Transactions',
#attach_query_result_as_file = 1,
#query_attachment_filename = 'sample.csv',
#query_result_separator = '',
#query_result_no_padding = 1
Most probably the error was due to 'File size limit was exceeded'.
You can set the file size (in bytes) using the following command:
EXECUTE msdb.dbo.sysmail_configure_sp 'MaxFileSize', '10000000';
you can get all set values by executing the following:
exec msdb.dbo.sysmail_help_configure_sp

Sending query results via email via email attachment every first day of every month on SQL Server 2012

My requirement:
Send the query result via email attachment on first day of every month.
The work I've been doing manually:
I have to run this query every first day of each month by changing the date range.
Then I export the result acquired in .csv format and send this csv file as an attachment
I needed suggestions from you people on how shall I automate this process:
Shall I set up a Job on SQL Server 2012, but yes, the I'll have to modify the date range.
Please suggest on how to move forward.
Any help, much appreciated.
As you mentioned, Create a Job and schedule it to run on first day of every month. Considering you have enough knowledge on creating a job.
Go to Job properties -> schedules -> and make the following setting
Occurs every first day of every 1 month(s) at 12:00:00. Schedule will
be used starting on 07-12-2016.
Change the timing(at which time it should run on first day of month) based on your business requirement. It can be set under Daily frequency-> Occurs once at:
This process can also be automated by another way by using a Windows batch file.You can schedule it using Windows scheduler.
Below will be contents of batch file
Echo off
sqlcmd -u <username> -p <password> -S <server name> -d <database name> -i <sql file location> -o <output result file location>
Powershell.exe -executionpolicy remotesigned -File <location of powershell file>
The powershell file trigger an email when bat file is run.
Contents of powershell file
$smtpserver = "<smtp server name>"
$from="mail id"
$to="<mail id"
$a = Get-Date
$subject= "<subject line> `r"+ $a.Month +"/"+$a.Day +"/"+ $a.Year
$body=""
$attachment="File location"
Thanks,`n "
$mailer = new-object Net.Mail.SMTPclient($smtpserver)
$msg = new-object Net.Mail.MailMessage($from,$to,$subject,$body,$data1,$a)
$msg.IsBodyHTML = $true
$mailer.send($msg)
I use SQL Agent for send results via email like this:
/*
First you should set up SQL Mail Profile.
Please change #dbName, #SQLscript, #mailbody and mail account values. When changing your #SQLscript value be careful that replace (with CTRL+H) single quota (') to double quotas ('').
*/
DECLARE #dbName nvarchar(50), #SQLscript nvarchar(4000), #subject varchar(100), #mailfrom varchar(100), #mailbody nvarchar(4000), #jobName varchar(100)
SELECT #jobName = name from msdb..sysjobs where job_id = $(ESCAPE_NONE(JOBID))
SELECT #mailfrom = ##SERVICENAME + ' <' + cast(SERVERPROPERTY('ComputerNamePhysicalNETBIOS') as varchar(50)) + '#domain.com>'
SELECT #subject = N'SQL Server Job Result [Job: ' + #jobName + ']'
SELECT #dbName = 'Database'
SELECT #SQLscript = '
INSERT INTO [Database].[Schema].[Table] (
Column1
,Column2
) VALUES (
''Value1''
,''Value2'' )
'
SELECT #mailbody = N'
Depending on case number 1234-5678-90 your script executed on <b>' + ##SERVERNAME + N'</b> instance and <b>' + #dbName + '</b> database. Script info and results are shown below. <br><br>' +
'<b>Script: </b><br>' + #SQLscript + '<br><br>' +
'<b>Result: </b><br>'
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'sqlmailprofile',
#recipients = '<mail1#domain.com>;<mail2#domain.com>',
#copy_recipients = '<mail3#domain.com>',
#from_address = #mailfrom,
#reply_to = '<mail3#domain.com>',
#subject = #subject,
#body = #mailbody,
#body_format = 'HTML',
#importance = 'HIGH',
#execute_query_database = #dbName,
#query = #SQLscript
/* If you want to send results with attached file:
#attach_query_result_as_file = 1,
#query_attachment_filename = 'script_output.csv',
#query_result_separator=#tab,
#query_result_width =32767,
#query_result_no_padding=1,
#exclude_query_output=1,
#query_result_header=1
*/

How to send string result as attachment in stored procedure sql server

i need to send a attachment to user, so i found query as attachment is possible but , my requirement is to send the SP result #V_Message variable as attachment within #attach_query_result_as_file , can any one help me in this issue
I don't have enough reputation points and cannot leave comments, hence it is here:
If all you need to send an email with well-formed attachments, then you should look into
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'whateverDefaultMailProfile',
#recipients = 'user#domain.com',
#subject = 'Email from SQL Server',
#body = 'Enter text here or use a #text var for body.',
#query= 'exec your_stored_procedure'
#file_attachments = 'C:\filename.ext'
your_stored_procedure should dump the output to c:\filename.ext file. You can attach multiple files to multiple recipients from within a single invocation of sp_send_dbmail. Hope this helps.
Yes. you can send attachment via email using (sp_send_dbmail stored proc)
Refer Example below. Hope this helps
EXEC sp_send_dbmail #profile_name='default',
#recipients='dev#null.com',
#subject=#SUB,
#body=#BODY,
#query= 'SELECT [MID],[HID],[MeC],[LC],[RowCDate]
FROM [JBC].[dbo].[Table1] WHERE RowCDate >= GETDATE()
',
#attach_query_result_as_file=1,
#query_attachment_filename = 'Results.csv',
#query_result_separator = ','

Sending File through database Mail

I am trying to sending a file through Database mail , when i execute the query below without #query option mail is triggered but when i include the #query option i get the error mentioned.
if ##rowcount >0
EXEC msdb.dbo.sp_send_dbmail #profile_name = ' Errormail',#recipients='arunkumarb#mobiusservices.in;',
#subject = 'A new Record created in the SSORunError Log Table' ,
#body = 'A new Record created in the SSORunError Log Table' ,
#query = 'select * from ip',
#attach_query_result_as_file = 1,
#query_result_width = 4000,
#query_attachment_filename = 'Details.txt'
Error Message :
Msg 22050, Level 16, State 1, Line 0
Error formatting query, probably invalid parameters
Msg 14661, Level 16, State 1, Procedure sp_send_dbmail, Line 504
Query execution failed: Msg 208, Level 16, State 1, Server , Line 1
Invalid object name 'ip'.
Thanks in advance
Try using fully qualified name for the table:
SELECT * FROM yourDatabase.yourSchemaName.ip
You can also set #execute_query_database parameter of your call to sp_send_dbmail to contain the name of your database (although I think that using fully qualified name should be enough).