SQL : 遍历文件夹并发送带有附件和存档的电子邮件 file.Exec sp_send_dbmail

SQL : Loop through folder and Send email with attachment and archive file.Exec sp_send_dbmail

大家好 - 我正在尝试创建以下存储过程,患者应在其中通过电子邮件接收附件文件。

文档位于文件夹中,每个文档文件名(示例:LetterPatient_12345)都附加了患者 ID (12345)。如何创建存储过程以在文件夹中查找患者 ID。 以下是按患者 ID 存在所有文档的文件夹。

W:\Files\Shared\Letters\Test

LetterPatient_12345

LetterPatient_56789

LetterPatient_10112

这是插入了上述查询的完整存储过程:

Declare @From nvarchar(max) = 'Dev <Development@un.org>'

DECLARE @Mail_Profile_Name VARCHAR(100)

SET @Mail_Profile_Name='DoNotReply'

DECLARE @MessageBody NVARCHAR(Max)

DECLARE @RecipientsList NVARCHAR(max) 

DECLARE @MailSubject NVARCHAR(500) = 'Reports'

DECLARE @EmailID INT

DECLARE @FirstName varchar(100),@LastName varchar(100)

DECLARE Email_cursor CURSOR FOR

Select distinct  PE.EmailAddress, lr.PatientFirstName, lr.PatientLastName, 
Lr.PatientId   from Letterrequest lr

Left join Patient PT on PT.PatientId = Lr.PatientId

Left join PatientEmail PE ON PE.PatientId = lr.PatientId

where 1=1

and PT.AssistanceCommunicationMethodId = 1

and PE.PreferredEmailIndicator = 1

and PE.InactiveDateTime IS NULL


OPEN Email_cursor 

FETCH NEXT FROM Email_cursor  INTO @RecipientsList,@FirstName,@LastName

WHILE @@FETCH_STATUS = 0

  BEGIN


  SET @MessageBody = 'Dear ' + @FirstName + COALESCE(' ' + @LastName,'') + CHAR(13) + CHAR(10) + 'Please find the letter attached '


  EXEC msdb.dbo.sp_send_dbmail

    @profile_name = @Mail_Profile_Name,

    @recipients = @RecipientsList,

    @body = @MessageBody,

    @subject = @MailSubject,

@Body_Format = 'HTML' ,

@from_address  = @From;




FETCH NEXT FROM Email_cursor  INTO @RecipientsList,@FirstName,@LastName

  END

CLOSE Email_cursor

DEALLOCATE Email_cursor

首先,您需要调整光标并添加一个@PatientId 变量。

FETCH NEXT FROM Email_cursor INTO @RecipientsList,@FirstName,@LastName,@PatientId

以此为参考:How to list files inside a folder with SQL Server

在使用 xp_DirTree 存储过程时,下面是一个如何完成您所追求的目标的示例:

DECLARE @FilePath NVARCHAR(500);
DECLARE @FileAttachment NVARCHAR(MAX) = ''
DECLARE @PatientID INT

SET @PatientID = 20181002

SET @FilePath = N'W:\Files\Shared\Letters\Test';

DECLARE @FileList TABLE
    (
        [FileName] NVARCHAR(500)
      , [depth] INT
      , [file] INT
    );

--using xp_DirTree:
--Parameters:
--directory - This is the directory you pass when you call the stored procedure; for example 'D:\Backup'.
--depth  - This tells the stored procedure how many subfolder levels to display.  The default of 0 will display all subfolders.
--isfile - This will either display files as well as each folder.  The default of 0 will not display any files.

--This gets a list of ALL files in your directory
--This before your cursor
INSERT INTO @FileList (
                          [FileName]
                        , [depth]
                        , [file]
                      )
EXEC [master].[sys].[xp_dirtree] @FilePath
                               , 1
                               , 1;


--Add this code inside your cursor to filter on only those files that contain the @PatientId and build out the string of files to attach.
SELECT @FileAttachment = @FileAttachment + @FilePath + '\' + [FileName] + ';'
FROM   @FileList
WHERE PATINDEX('%' + CONVERT(NVARCHAR, @PatientID) + '%', [FileName]) <> 0

--This just removes the trailing ;
SET @FileAttachment = SUBSTRING(@FileAttachment,1,LEN(@FileAttachment)-1)

--Then you can add the @file_attachments parameter to sp_send_dbmail and set that equal to the @FileAttachment variable.
SELECT @FileAttachment

只是关于文件所在目录的附注。 运行 SQL 服务器服务的帐户必须有权访问文件所在的目录或 SQL 代理,或者如果 运行 通过代理帐户。我假设 "W:\" 位于执行此存储过程的服务器上。如果没有,它需要可以从服务器和帐户访问。