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;