Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 使用路径和文件名作为变量,在sp_send_dbmail中附加多个文件_Sql Server_Email Attachments - Fatal编程技术网

Sql server 使用路径和文件名作为变量,在sp_send_dbmail中附加多个文件

Sql server 使用路径和文件名作为变量,在sp_send_dbmail中附加多个文件,sql-server,email-attachments,Sql Server,Email Attachments,我想在sp_send dbmail中附加多个文件,但文件名和路径在更改时是可变的。下面是我的SQl脚本。在按照下面的脚本运行之后,我在邮件中只得到一个文件作为附件,但有多个文件以ERR作为名称开头 有人能帮我吗 declare @files table (Filename varchar(1000)) declare @filename varchar(500) declare @foldername varchar(1000) = REPLACE(CONVERT(varchar(10), GE

我想在sp_send dbmail中附加多个文件,但文件名和路径在更改时是可变的。下面是我的SQl脚本。在按照下面的脚本运行之后,我在邮件中只得到一个文件作为附件,但有多个文件以ERR作为名称开头

有人能帮我吗

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 = 'person@example.com'
, @subject = 'sub'
, @body = 'pfa'
, @file_attachments = @filepath;

根据上的Microsoft文档,可以使用分号分隔

[@file\u attachments=]'file\u attachments'是以分号分隔的 要附加到电子邮件的文件名列表。列表中的文件 必须指定为绝对路径。附件列表的类型为 nvarcharmax。默认情况下,数据库邮件将文件附件限制为1 每个文件MB

您可以尝试用分号连接所有文件名