Formatting within exec msdb.dbo.sp_send_dbmail

I have an exec msdb.dbo.sp_send_dbmail which works well.  However, I would like to know can the output in the @Query be formatted so the output lines up and are easier to read.

Here is what the email receiver sees:

Crowe Transaction Balance Operations Summary Report Transactions Balance Crowe Bad Debt Balance Operations Summary Report BD Balance
------------------------- ---------------------------------------------- ---------------------- ------------------------------------
               -372232.49                                     -372232.49                    .00                                  .00

I have embedded the sql code.

Is there a way to control the format?

Thanks

Glen
exec msdb.dbo.sp_send_dbmail
   @profile_name='GlenMail',
   @recipients ='gpspow55@gmail.com',
   @subject ='Daily ATB SQL table Update Status',
   @body = 'The processing of the DAILY_ATB_BALANCE table has processed successfully.
   Below is a reconcilation of the Operations Summary Collections Transactions and Bad Debt Transactions Balance with the transactions
   sent to Crowe.
   
   ',
   @query = '
   Declare @CROWETxnBal as numeric(15,2);
Declare @TxnBal as numeric(15,2);
Declare @CROWEBDBal as numeric(15,2);
Declare @BDBal as numeric(15,2);

Select @CROWETxnBal = convert(numeric(15,2),coalesce(SUM(TxnAmount),0)) 
		   from livedb.dbo.tbl_RCA_DAILY_TXN_DETAIL
Select @TxnBal =CONVERT(numeric(15,2),coalesce([TAmount],0))
			FROM [livedb].[dbo].[vw_DAILY_OP_TXNS_TOT]
Select @CROWEBDBal = convert(numeric(15,2),coalesce(SUM(Amount),0)) 
		   from livedb.dbo.tbl_RCA_BD_TRANSFER_DATA
Select @BDBal =CONVERT(numeric(15,2),coalesce([BAmount],0))
			FROM [livedb].[dbo].[vw_DAILY_OP_TXNS_TOT]

Select @CROWETxnBal as [Crowe Transaction Balance], @TxnBal as [Operations Summary Report Transactions Balance],
@CROWEBDBal as [Crowe Bad Debt Balance], @BDBal as [Operations Summary Report BD Balance]
	'

Open in new window

GPSPOWAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Koen Van WielinkBusiness Intelligence SpecialistCommented:
While your formatting options are certainly limited, if you only want to improve readability, you can get a long way with carriage returns/line feeds and tabs.
New lines can be forced like this:

Declare @example1 nvarchar(max);

Set @example1 = 'This is an example of a text. This is line 1.'
Set @example1 = @example1 +  char(10) + char(13)
Set @example1 = @Example1 + 'This text will appear on line 2.'

select @example1;

Open in new window


When testing this option in SSMS, make sure to right click on the query and select Results To -> Results to text.

To use a tab works similar:

Declare @example2 nvarchar(max);

Set @example2 = 'This will be a header line: ' + char(10) + char(13) + char(10) + char(13)

Set @example2 = @example2 + 'Column1' + char(9) + 'Column2' + char(9) + 'Column3'
Set @example2 = @example2 + char(10) + char(13)
Set @example2 = @example2 + 'abc' + char(9) + char(9) + 'def' + char(9) + char(9) + 'ghi'

Select @example2;

Open in new window


Of course if you're trying to format a table and you don't know how long each value will be this can get a little tricky in terms of column alignment. But you should be able to work around that.
Hope this helps.
Deepak ChauhanSQL Server DBACommented:
I have made some changes in your script, please check this should be what you need.

Declare @CROWETxnBal numeric(15,2);
Declare @TxnBal numeric(15,2);
Declare @CROWEBDBal numeric(15,2);
Declare @BDBal numeric(15,2);

Declare @xml NVARCHAR(MAX)
DECLARE @body NVARCHAR(MAX)

set @CROWETxnBal = (select convert(numeric(15,2),coalesce(SUM(TxnAmount),0)) from livedb.dbo.tbl_RCA_DAILY_TXN_DETAIL)
set @TxnBal =(select CONVERT(numeric(15,2),coalesce([TAmount],0)) FROM [livedb].[dbo].[vw_DAILY_OP_TXNS_TOT])
set @CROWEBDBal =(select convert(numeric(15,2),coalesce(SUM(Amount),0)) from livedb.dbo.tbl_RCA_BD_TRANSFER_DATA)
set @BDBal =(select CONVERT(numeric(15,2),coalesce([BAmount],0)) FROM [livedb].[dbo].[vw_DAILY_OP_TXNS_TOT])

SET @xml = CAST(( SELECT @CROWETxnBal AS 'td','',@TxnBal AS 'td','',
       @CROWEBDBal AS 'td','', @BDBal AS 'td'
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))


SET @body ='<html><body><H3>The DAILY_ATB_BALANCE processing status</H3>
   The processing of the DAILY_ATB_BALANCE table has processed successfully.
   Below is a reconcilation of the Operations Summary Collections Transactions and Bad Debt Transactions Balance with the transactions
   sent to Crowe.
   <table border = 1> 
   <tr>
   <th> Crowe Transaction Balance </th>
   <th> Operations Summary Report Transactions Balance </th> 
   <th> Crowe Bad Debt Balance </th> 
   <th> Operations Summary Report BD Balance </th></tr>'    
 
SET @body = @body + @xml +'</table></body></html>'
exec msdb.dbo.sp_send_dbmail
   @profile_name='GlenMail',
   @recipients ='gpspow55@gmail.com',
   @subject ='Daily ATB SQL table Update Status',
   @body = @body,
   @body_format ='HTML',

Open in new window

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
GPSPOWAuthor Commented:
Thanks

This gave me what I was looking for.

I added a few more fields and got it to line up correctly

I will need to read up on HTML formatting.

Glen
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.