Hi Everyone – I am trying to create the below Stored Procedure where the patient should receive the attachment document via email.
The documents are in the folder and each document filename (Example: LetterPatient_12345) has a patient ID (12345) appended to it. How do I create the Stored Procedure to look for patient ID in the folder. Below is the folder where all the documents exist by patient ID.
W:\Files\Shared\Letters\Test
LetterPatient_12345
LetterPatient_56789
LetterPatient_10112
Here is the complete Stored Procedure with the above query plugged in:
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