Brad is also available on 'Simple-Talk' and 'Twitter'

select
distinct
ltrim(isnull(Title,'') + ' ' + FirstName + ' ' + LastName)CustomerName,
Phone,
EmailAddress
from
Sales.vIndividualCustomer
where
StateProvinceName = 'New South Wales' Declare @Body as varchar(max)
set @Body = '<html><body>' +
'<h1>NSW Customers</h1>
' + '<table border="1">'
DECLARE Customers Cursor FAST_FORWARD FOR
select
distinct
ltrim(isnull(Title,'') + ' ' + FirstName + ' ' + LastName) CustomerName,
Phone,
EmailAddress
from
Sales.vIndividualCustomer
where
StateProvinceName = 'New South Wales'
Declare @CustomerName varchar(200),
@Phone varchar(25),
@EmailAddress varchar(50)
open Customers
Fetch Next From Customers into @CustomerName, @Phone, @EmailAddress
while @@Fetch_STatus = 0
begin
set
@Body = @body + '<tr> '
+ '<td>' + @CustomerName + '</td>'
+ '<td>' + @Phone + '</td>'
+ '<td>' + @EmailAddress + '</td>'
+ '</tr>'
Fetch Next from Customers into @CustomerName, @Phone, @EmailAddress
end
close Customers
Deallocate Customers
set @body = @body + '</table>'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'AdventureWorksProfile',
@recipients='adventureJo@adventure-works.com',
@subject = 'NSW Customers',
@body = @body,
@body_format = 'HTML' ;
DECLARE @body varchar(max),
@Subject varchar(255)
SET @body =
N'<html><body><H1>NSW Customers</H1>' +
'<table border="1">' +
'<tr><th>Customer Name</th>' +
'<th>Phone</th>' +
'<th>EmailAddress</th>' +
(select
Distinct
ltrim(isnull(Title,'') + ' ' + FirstName + ' ' + LastName) td, '',
Phone td, '',
EmailAddress td, ''
from
Sales.vIndividualCustomer
where
StateProvinceName = 'New South Wales'
FOR XML PATH('tr')) +
N'</table></body></html>'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'AdventureWorksProfile',
@recipients='adventureJo@adventure-works.com',
@subject = 'NSW Customers',
@body = @body,
@body_format = 'HTML' ;