Sql server SQL电子邮件附件疑难解答
我有一个脚本,将文件作为附件拉入电子邮件并发送出去。它最近已经停止工作。 我正在提取的文件名为“DTH201509240918.xlsx”,最后4个数字是随机的,所以我使用“^.*$”。这些文件在源文件夹中,但我总是得到“找不到文件”。 如果您有任何关于文件未被提取原因的帮助,我们将不胜感激Sql server SQL电子邮件附件疑难解答,sql-server,tsql,sp-send-dbmail,Sql Server,Tsql,Sp Send Dbmail,我有一个脚本,将文件作为附件拉入电子邮件并发送出去。它最近已经停止工作。 我正在提取的文件名为“DTH201509240918.xlsx”,最后4个数字是随机的,所以我使用“^.*$”。这些文件在源文件夹中,但我总是得到“找不到文件”。 如果您有任何关于文件未被提取原因的帮助,我们将不胜感激 ALTER Procedure [dbo].[DSEmail] as begin declare @to varchar(1000) ,@sub varchar(
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\u 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'
之后,您可以调整上述代码,以包含希望附加到电子邮件中的确切文件名。你好,拉尔夫,谢谢您的建议。我有点搞不清楚要添加什么。因此,这段代码应该替换我在原始代码中标记为@attachments的正则表达式?我不知道要删除和添加什么,或者“N”是什么意思。如果你能进一步解释,我将不胜感激。非常感谢。