sql server - How to avoid the column name with lines displaying while calling sp_send_dbmail? -
i'm new stored procedure. i've tried mail reading table , did below query
create procedure mailtouser declare @data varchar(max), @user varchar(max) set @data='set nocount on;select col_name tbl_name id=1;set nocount off;' set @user='user@example.com' set @query= exec sp_send_dbmail @profile_name = 'profile', @recipients = @user, @subject = 'automail', @execute_query_database = 'database', @query = @data; end end
exec mailtouser go
while executing script, i'm getting mail
as
col_name ------------------------------------------------------------------------------------------------------------ datas.
how avoid col_name , lines. want data alone in mail.
first, naming database [database]? should [adventureworks2012].
second, query execute , return results. not need @execute_query_database if use 3 part notation [adventureworks2012].[person].[address].
third, there other parameters go along option. check them out.
@query_result_header @query_result_width @query_result_separator @exclude_query_output
on other hand, if want take full control, change body format html.
run query inside set statement format data way want.
-- send embedded html table containing query data declare @var_html nvarchar(max) ; set @var_html = n'<h1>work order report<h1>' + n'<table border="1">' + n'<tbody><tr><th>work order id</th><th>product id</th>' + n'<th>name</th><th>order qty</th><th>due date</th>' + n'<th>expected revenue</th></tr>' + cast ( ( select td = wo.workorderid, '', td = p.productid, '', td = p.name, '', td = wo.orderqty, '', td = wo.duedate, '', td = (p.listprice - p.standardcost) * wo.orderqty adventureworks2008r2.production.workorder wo join adventureworks2008r2.production.product p on wo.productid = p.productid duedate > '2006-04-30' , datediff(dd, '2006-04-30', duedate) < 2 order duedate asc, (p.listprice - p.standardcost) * wo.orderqty desc xml path('tr'), type ) nvarchar(max) ) + n'</tbody></table>' exec msdb.dbo.sp_send_dbmail @recipients='john@craftydba.com', @subject = 'work order list', @body = @var_html, @body_format = 'html' ;
sincerely
j
Comments
Post a Comment