我有一个脚本,将文件作为附件拉入电子邮件并将其发送出去。它最近停止了工作。我正在提取的文件名为"DTH201509240918.xlsx",最后4个数字是随机的,所以我使用"^.*$"。文件在源文件夹中,但我总是得到"没有找到文件"。如果你能告诉我为什么文件没有被拉出,我将不胜感激。
ALTER Procedure [dbo].[DSEmail] as begin
declare @to varchar(1000)
,@sub varchar(1000)
,@subdate varchar(10) = CONVERT(Varchar(10),GETDATE(),101)
,@bod varchar(1000)
,@filePath varchar(1000) = '\source'
,@fileDate varchar (8) = CONVERT(Varchar(8),GETDATE(),112)
,@attachments varchar(1000)
,@pathAndFile varchar (1000)
Set @attachments = 'DTH' + @fileDate + '^.*$'
Set @pathAndFile = @filePath + @attachments
Set @sub = 'dS Report ' + @subdate
Set @bod = '\source' + @attachments
Set @to = 'email@email.com'
IF dbo.fn_FileExists(@pathAndFile) = 1
exec msdb.dbo.sp_send_dbmail
@recipients = @to
,@subject = @sub
,@body = @bod
,@file_attachments = @pathAndFile
ELSE
exec msdb.dbo.sp_send_dbmail
@recipients = @to
,@subject = 'DS Error'
,@body = 'No file was found. Please check \source for today''s file.'
End
不能对master.dbo.fn_FileExists
使用正则表达式。下面的代码应该能帮你找到可以附加到电子邮件中的确切文件名:
declare @dir table
(
ID int identity(1, 1)
primary key
not null,
dirEntry nvarchar(max)
);
insert into @dir
(dirEntry)
exec xp_cmdshell 'dir ' + @filePath + N'*.*';
select d.dirEntry
from @dir as d
where d.dirEntry like N'DTH' + @fileDate + '%.xlsx'
之后,您可以调整您的上述代码,以包括确切的文件名,您希望附加到您的电子邮件。