Link to home
Start Free TrialLog in
Avatar of Lapchien
LapchienFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Send SQL Email With Attachment (Location in Path Field)

I have a table that stores the location of a document (Documents) and another that stores the email address (engineers).  I want to know if it's possible to send an email from sql with an attachment?
ASKER CERTIFIED SOLUTION
Avatar of Om Prakash
Om Prakash
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lapchien

ASKER

Thanks, so I have the following code, it only outputs the first record, how can I get it to skip through all the records and send multiple emails out?




DECLARE
	@EmailAddress varchar(255),
	@Attachments varchar(255)

SELECT
	@EmailAddress = Engineers.EmailAddress, 
	@Attachments = [Path]
FROM 
	TASDocument
	inner join Documents on Documents.DocId = TASDocument.DocId
	inner join Engineers on Engineers.EngineerId = TASDocument.EngineerId

where Documents.DocTypeId = 50
and Documents.PayBatchNo = 'PB0047'



EXEC msdb.dbo.sp_send_dbmail
@recipients='@EmailAddress',
@body='Message Body', 
@subject ='Message Subject',
@profile_name ='Administrator',
@file_attachments=@Attachments;

Open in new window