Friday, February 24, 2012

Database Mail Formatting

I created the following, which emails out a query from a SQL 2005 database.

It works fine, but is there anyway to change the formatting to look more like a table?

Such as:

ESSSEQUENCE: 2000

MaterialType: 900

ESSPrefix: 01

PartDesc: Test

Project: 2833

PM: Jeff House

Here's the following code I'm using that sends the data as text and you can't tell what column heading goes with the data because it's kind of all over the place.

Any help would be appreciated..


declare @.q as varchar(255)

select @.q = 'select ESSSequence, MaterialType, ESSPrefix, PartDesc, Project, PM

from ESSPartNumLog

where ESSSequence = (SELECT MAX(esssequence) FROM esspartnumlog) '


exec msdb.dbo.sp_send_dbmail

@.recipients = 'tengel@.pivotint.com',

@.Profile_name = 'DatabaseEmail2',

@.subject = 'New record added in ESSPartNumLog',

@.query = @.q,

@.execute_query_database = 'Engineering'

You should be able to concat everything together into one string with formatting..something like this (if you've got any fields that are not strings you may have to cast or convert)

select 'ESSSequence:' + Esssequence + Char(13) + Char(10) + 'MaterialType:'+ MaterialType + Char(13) + Char(10)

etc....

RKS

No comments:

Post a Comment