SSIS, retrieve query result and send email
User would like to have a email like this in the email body SalesmanA 10000 Salesmanb 63000 I have setup an Execute SQL task to get the result and also setup a for each loop container to loop the result. But now, I have no idea that how to send email like above format. Kindly advise .
May 23rd, 2011 4:01am

Perhaps something like that EXEC msdb.dbo.sp_send_dbmail @profile_name = 'John Smith', @recipients = 'jjj@hotmail.com', @query = 'SELECT COUNT(*) FROM AdventureWorks.Person.Address', @subject = 'Error Log Count', @attach_query_result_as_file = 1 ; Best Regards, Uri Dimant SQL Server MVP http://dimantdatabasesolutions.blogspot.com/ http://sqlblog.com/blogs/uri_dimant/
Free Windows Admin Tool Kit Click here and download it now
May 23rd, 2011 4:06am

Hi, You can use the send mail task present in SSIS. The values you want to see in mail text can be passed as parameters as below @SalesMan @SalesCount Hope this helps !!! Thanks, Vaibhav
May 23rd, 2011 4:29am

The request need the result in the email body, not as an attachement.
Free Windows Admin Tool Kit Click here and download it now
May 23rd, 2011 4:30am

do you want to make a single mail per all information retrieved by execute sql task? if yes, just create a package scope, object type variable, name it SQLResults for example, then set execute sql task result set property as Full result set and set result set tab, as result name with 0 , and variable with SQLResults then in foreach loop editor, set enumerator as ADO and set source variable as SQLResults create another variable(s) of appropriate type. ( forexample field1,field2,..) then you can set them in variable mappigns tab of foreach loop editor, with set index as column orders and variable names as you need. add another variable in package scope, string type, name it BodyMsg then add a script task inside foreach container, set BodyMsg as ReadWriteVariables, and set field1,field2,... as ReadOnlyVariable, then click on edit script you can use this script to update BodyMsg in each iteration: Dts.Variable["User::BodyMsg"].Value=Dts.Variable["User::BodyMsg"].Value.ToString()+"\\r\\n"+Dts.Variable["User::field1"].Value.ToString() ; then after foreach container, add a send mail task and set source body as BodyMsg variable. http://www.rad.pasfu.com
May 23rd, 2011 5:14am

The request need the result in the email body, not as an attachement. The MSDN page about sp.send_db_mail literally states the following about the @query parameter: "Is a query to execute. The results of the query can be attached as a file, or included in the body of the e-mail message"MCTS, MCITP - Please mark posts as answered where appropriate.
Free Windows Admin Tool Kit Click here and download it now
May 23rd, 2011 5:49am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics