Building content for email to send email in Tabular Format

Drop table #mytemp
declare @au_id varchar( 100 )
DECLARE @xml NVARCHAR(MAX)
DECLARE @body NVARCHAR(MAX)
set rowcount 0
select distinct Department into #mytemp from [HumanResources].[vEmployeeDepartment] where Department in ('Sales','Production')
set rowcount 1
select @au_id = Department from #mytemp
while @@rowcount <> 0
begin
    set rowcount 0
    Drop table #mytempTemp
      select Top 5 * into #mytempTemp from [HumanResources].[vEmployeeDepartment] where Department = @au_id
      SET @xml = CAST(( SELECT [FirstName] AS 'td','',[LastName] AS 'td','',
      [Department] AS 'td'
      FROM  #mytempTemp ORDER BY LastName, FirstName
      FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))
      SET @body ='<html><body><H3>Details as follows:</H3>
      <table border = 1>
      <tr>
      <th> First Name </th> <th> Last Name </th> <th> Department </th></tr>'    
       SET @body = @body + @xml +'</table></body></html>'
      EXEC msdb.dbo.sp_send_dbmail
      @profile_name = 'Notifications', -- replace with your SQL Database Mail Profile
      @body = @body,
      @body_format ='HTML',
      @recipients = '@Emailaddress', -- replace with your email address
      @subject = 'E-mail in Tabular Format - Testing' ;

    delete #mytemp where Department = @au_id
    delete #mytempTemp where Department = @au_id
    set rowcount 1
    select @au_id = Department from #mytemp
end
set rowcount 0
WestminsterWebDevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WestminsterWebDevAuthor Commented:
I have solved this problem. Here I posted the solutions.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.