0

I would like to attach multiple files in sp_send dbmail but filename and path is variable as it changes. Below is my SQl script. After running below script I am getting only 1 file on mail as attachment but there are multiple files which starts with ERR as name.

Can someone please help me here?

declare @files table (Filename varchar(1000))
declare @filename varchar(500)
declare @foldername varchar(1000) = REPLACE(CONVERT(varchar(10), GETDATE()-1, 112), '/', '')
declare @folderpath varchar(500)
declare @filepath varchar(1000)

set @folderpath = 'xp_cmdshell ''dir E:\Batch\Error_Logs\'+@foldername+ ' /b'''

insert into @files exec (@folderpath)

SELECT @filename = Filename from @files where Filename like 'ERR_%'

set @filepath = 'E:\Batch\Error_Logs\'+@foldername+'\'+@filename

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'DB_Mail'
, @recipients = '[email protected]'
, @subject = 'sub'
, @body = 'pfa'
, @file_attachments = @filepath;
0

1 Answer 1

1

According to Microsoft documentation on sp_send_mail, you can doing it with delimiting (separating) it with semi colons (;).

[ @file_attachments= ] 'file_attachments' Is a semicolon-delimited list of file names to attach to the e-mail message. Files in the list must be specified as absolute paths. The attachments list is of type nvarchar(max). By default, Database Mail limits file attachments to 1 MB per file.

You can try to concatenate all the file names with semi colons.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.