Dashes appearing in EMAILS sent by Database Mail

I have created a job which executes a procedure which executes and creates logs of StartTime and EndTime and records affected. The last step of the job sends mails to recipients from the logs table. Though I get the info correctly I get all annoying dashes for 4-5 lines between each line of message.

Please help me to remove the dashes.

Below is the code i use to send mails.

EXEC msdb.dbo.sp_send_dbmail 
  @profile_name='SQL Mail Service',
  @recipients='biscribbler@gmail.com',
  @copy_recipients='biscribbler@gmail.com',
  @subject='SOFT SCRUB Completed for LiveDB',
  @query='exec PCMArchiva..APS_MailStats'

Procedure APS_MailStats contain below code:

DECLARE @tid INT = (SELECT MAX(TID) FROM APS_Audits)
DECLARE @tih INT =(SELECT DATEDIFF(HH ,StartTime ,EndTime) FROM APS_Audits WHERE TID = @tid) 
,@tin INT = (SELECT DATEDIFF(N ,StartTime ,EndTime)%60 FROM APS_Audits WHERE TID = @tid)

SELECT 'Hi All,'
SELECT 'PCM SoftScrub Process for LiveDB has completed successfully'
SELECT 'Summary : ' + LTRIM(RTRIM(comments)) FROM APS_Audits WHERE CONVERT(DATE ,EndTime) = CONVERT(DATE ,GETDATE())
AND TID = (SELECT MAX(TID) FROM APS_Audits)
SELECT 'Time Taken for this execution :' +CONVERT(VARCHAR ,@tih)+':'+CONVERT(VARCHAR ,@tin)

September 14th, 2015 9:59am

Try printing the values of the variables. This will help you to determine where they are coming from.

Once you know which variable is responsible, you can delve deeper into why.

At a glance, I'd be suspicious of the @query value.

Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 10:24am

Hi imbest,

The hyphens are the separating line between table headers and rows . Let's try "SELECT 'Hi All,' AS GREET", it leads to "GREET ------- Hi All" in your mail finally. In your case hyphens can be suppressed with a tricky workaround using union all.
SELECT 'Hi All,'
UNION ALL
SELECT 'PCM SoftScrub Process for LiveDB has completed successfully'
UNION ALL
SELECT 'Summary : ' + LTRIM(RTRIM(comments)) FROM APS_Audits WHERE CONVERT(DATE ,EndTime) = CONVERT(DATE ,GETDATE())
AND TID = (SELECT MAX(TID) FROM APS_Audits)
UNION ALL
SELECT 'Time Taken for this execution :' +CONVERT(VARCHAR ,@tih)+':'+CONVERT(VARCHAR ,@tin) 

But there're still some hyphens can't be suppressed. If you don't like any hyphens please change your stored procedure and sp_send_dbemail as below.

	ALTER PROC yourProc(@bodyText VARCHAR(MAX) OUT)
	AS
	SET NOCOUNT ON

	DECLARE @TEXT VARCHAR(MAX)
	DECLARE @CRLF CHAR(2)=CHAR(13)+CHAR(10)
	;WITH Cte AS(
	SELECT 'Hi All,'  AS line
	UNION ALL
	SELECT 'PCM SoftScrub Process for LiveDB has completed successfully' )
	SELECT @bodyText = stuff((SELECT LINE + @CRLF
                        FROM Cte                        
                        FOR XML PATH(''),TYPE).value('.','varchar(max)'),1,0,'')
GO
--------
DECLARE @bodyText VARCHAR(MAX)
EXEC TESTDB.DBO.yourProc @bodyText OUT

EXEC msdb.dbo.sp_send_dbmail
  @profile_name='SQL Mail Service',
  @recipients='biscribbler@gmail.com',
  @copy_recipients='biscribbler@gmail.com',
  @subject='SOFT SCRUB Completed for LiveDB',
  @body = @bodyText
  --@query='exec PCMArchiva..APS_MailStats'
  

If you have any question, feel free to let me know.
September 15th, 2015 3:28am