SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


sp_send_dbmail is not giving proper formate in txt attachement of SQL query output


sp_send_dbmail is not giving proper formate in txt attachement of SQL query output

Author
Message
Megha P
Megha P
Old Hand
Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)

Group: General Forum Members
Points: 350 Visits: 2501
Hi ,
I have following code to execute. i want to have functionality to send email for the following query output in text attachment, please note that query output contains XML result for one column.

It sends mail successfully but it is not properly formatted .Following is the code and Output

Code:
declare @subj varchar(100)
declare @cmd varchar(5000)
DECLARE @file varchar(100)
SET @cmd = 'SET QUOTED_IDENTIFIER ON
GO
; WITH
XMLNAMESPACES
(
DEFAULT ''http://schemas.microsoft.com/sqlserver/2004/07/showplan''
)
, CachedPlans
(
MissingIndexesPortion,
QueryPlan,
DatabaseName,stmt,DbId,
ObjectName,
--QueryText,
RefCounts,
UseCounts,
SizeBytes,
CacheObjectType,
ObjectType
)
AS
(
SELECT
RelOp.op.query(''.'') AS MissingIndexesPortion,
qp.query_plan AS QueryPlan,
Db_name(qp.dbid),
op.value(''(//MissingIndex/@Database)[1]'', ''VARCHAR(128)'') + ''.'' +
op.value(''(//MissingIndex/@Schema)[1]'', ''VARCHAR(128)'') + ''.'' +
op.value(''(//MissingIndex/@Table)[1]'', ''VARCHAR(128)'') stmt, qp.dbid,
Object_name (qp.objectid,qp.dbid),
--st.[text] AS QueryText,
cp.refcounts AS RefCounts,
cp.usecounts AS UseCounts,
cp.size_in_bytes AS SizeBytes,
cp.cacheobjtype AS CacheObjectType,
cp.objtype AS ObjectType
FROM
sys.dm_exec_cached_plans cp
CROSS APPLY
sys.Dm_exec_sql_text(cp.plan_handle) st
CROSS APPLY
sys.Dm_exec_query_plan(cp.plan_handle) qp
CROSS APPLY
qp.query_plan.nodes(''//MissingIndexes'') RelOp (op)
)

SELECT Getdate(),
MissingIndexesPortion,
QueryPlan,
ObjectName AS procedurename,
stmt AS TableName,
--QueryText,
RefCounts,
UseCounts,
SizeBytes,
ObjectType
FROM
CachedPlans
WHERE CacheObjectType =''Compiled Plan''
ORDER BY
UseCounts;'
set @file = 'Missing.txt'


exec msdb.dbo.sp_send_dbmail
@profile_name = 'SQL2005_Email_Profile',
@recipients = 'AAA@mail.com',
@subject = 'Missing Index',
@query = @cmd,
@attach_query_result_as_file = 1,
@query_attachment_filename = @file ,
@query_result_separator = '; ',
@query_result_no_padding = 1


Output in Txt file :

;MissingIndexesPortion;QueryPlan;procedurename;TableName;RefCounts;UseCounts;SizeBytes;ObjectType
-;---------------------;---------;-------------;---------;---------;---------;---------;----------
2012-12-31 13:04:13.913;<p1:MissingIndexes xmlns:p1="http://schemas.microsoft.com/sqlserver/2004/07/showplan"><p1:MissingIndexGroup Impact="44.7183"><p1:MissingIndex Database="[Enhance]" Schema="[dbo]" Table="[gfn_commentThreadIndexing]"><p1:ColumnGroup Us
age="EQUALITY"><p1:Colum;<ShowPlanXML xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan" Version="1.0" Build="9.00.5000.00"><BatchSequence><Batch><Statements><StmtSimple StatementText=" CREATE PROCEDURE Griffin_getAllCommentThreadFo
rIndexing AS&#;Griffin_getAllCommentThreadForIndexing;[Enhance].[dbo].[gfn_commentThreadIndexing];2;2;131072;Proc

(1 rows affected)


Thanks,
Megha
Orlando Colamatteo
Orlando Colamatteo
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14813 Visits: 14396
Megha P (12/31/2012)
Hi ,
I have following code to execute. i want to have functionality to send email for the following query output in text attachment, please note that query output contains XML result for one column.

It sends mail successfully but it is not properly formatted .Following is the code and Output

Code:
DECLARE @subj VARCHAR(100)
DECLARE @cmd VARCHAR(5000)
DECLARE @file VARCHAR(100)
SET @cmd = 'SET QUOTED_IDENTIFIER ON;

WITH XMLNAMESPACES(DEFAULT ''http://schemas.microsoft.com/sqlserver/2004/07/showplan''),
CachedPlans
(
MissingIndexesPortion,
QueryPlan,
DatabaseName,stmt,DbId,
ObjectName,
--QueryText,
RefCounts,
UseCounts,
SizeBytes,
CacheObjectType,
ObjectType
) AS (
SELECT RelOp.op.query(''.'') AS MissingIndexesPortion,
qp.query_plan AS QueryPlan,
DB_NAME(qp.dbid),
op.value(''(//MissingIndex/@Database)[1]'', ''VARCHAR(128)'') + ''.'' + op.value(''(//MissingIndex/@Schema)[1]'', ''VARCHAR(128)'') + ''.''
+ op.value(''(//MissingIndex/@Table)[1]'', ''VARCHAR(128)'') stmt,
qp.dbid,
OBJECT_NAME(qp.objectid, qp.dbid),
cp.refcounts AS RefCounts,
cp.usecounts AS UseCounts,
cp.size_in_bytes AS SizeBytes,
cp.cacheobjtype AS CacheObjectType,
cp.objtype AS ObjectType
FROM sys.dm_exec_cached_plans cp
CROSS APPLY sys.Dm_exec_sql_text(cp.plan_handle) st
CROSS APPLY sys.Dm_exec_query_plan(cp.plan_handle) qp
CROSS APPLY qp.query_plan.nodes(''//MissingIndexes'') RelOp (op) )
SELECT GETDATE(),
MissingIndexesPortion,
QueryPlan,
ObjectName AS procedurename,
stmt AS TableName,
RefCounts,
UseCounts,
SizeBytes,
ObjectType
FROM CachedPlans
WHERE CacheObjectType = ''Compiled Plan''
ORDER BY UseCounts;'

SET @file = 'Missing.txt'

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SQL2005_Email_Profile',
@recipients = 'AAA@mail.com',
@subject = 'Missing Index',
@query = @cmd,
@attach_query_result_as_file = 1,
@query_attachment_filename = @file,
@query_result_separator = '; ',
@query_result_no_padding = 1;



Output in Txt file :

;MissingIndexesPortion;QueryPlan;procedurename;TableName;RefCounts;UseCounts;SizeBytes;ObjectType
-;---------------------;---------;-------------;---------;---------;---------;---------;----------
2012-12-31 13:04:13.913;<p1:MissingIndexes xmlns:p1="http://schemas.microsoft.com/sqlserver/2004/07/showplan"><p1:MissingIndexGroup Impact="44.7183"><p1:MissingIndex Database="[Enhance]" Schema="[dbo]" Table="[gfn_commentThreadIndexing]"><p1:ColumnGroup Us
age="EQUALITY"><p1:Colum;<ShowPlanXML xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan" Version="1.0" Build="9.00.5000.00"><BatchSequence><Batch><Statements><StmtSimple StatementText=" CREATE PROCEDURE Griffin_getAllCommentThreadFo
rIndexing AS&#;Griffin_getAllCommentThreadForIndexing;[Enhance].[dbo].[gfn_commentThreadIndexing];2;2;131072;Proc

(1 rows affected)


Thanks,
Megha

Are you sure that your code runs successfully? I am pretty sure that you cannot include GO in a SQL batch sent to Database Mail.

You said your output is in the wrong format...well what is the right format?

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Bhuvnesh
Bhuvnesh
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5174 Visits: 4076
try with another paramter [ @body_format= ] = 'HTML '

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
Megha P
Megha P
Old Hand
Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)Old Hand (350 reputation)

Group: General Forum Members
Points: 350 Visits: 2501
Hi,
i have already tried with html format but there was problem in convert from xml to varchar ,because of large xml ,it was truncated.
At last,i have changed my query to get the output in varchar datatype instead of xml..and now it is working properly in html format.

thank you..
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search