广告位招租!
知识无价,人有情,无偿分享知识,希望本条信息对你有用!
Exec msdb.dbo.sp_send_dbmail 参数详解
sp_send_dbmail
[ [ @profile_name = ] 'profile_name' ]
[ , [ @recipients = ] 'recipients [ ; n ]' ]
[ , [ @copy_recipients = ] 'copy_recipient [ ; n ]' ]
[ , [ @blind_copy_recipients = ] 'blind_copy_recipient [ ; n ]' ]
[ , [ @subject = ] 'subject' ]
[ , [ @body = ] 'body' ]
[ , [ @body_format = ] 'body_format' ]
[ , [ @importance = ] 'importance' ]
[ , [ @sensitivity = ] 'sensitivity' ]
[ , [ @file_attachments = ] 'attachment [ ; n ]' ]
[ , [ @query = ] 'query' ]
[ , [ @execute_query_database = ] 'execute_query_database' ]
[ , [ @attach_query_result_as_file = ] attach_query_result_as_file ]
[ , [ @query_attachment_filename = ] query_attachment_filename ]
[ , [ @query_result_header = ] query_result_header ]
[ , [ @query_result_width = ] query_result_width ]
[ , [ @query_result_separator = ] 'query_result_separator' ]
[ , [ @exclude_query_output = ] exclude_query_output ]
[ , [ @append_query_error = ] append_query_error ]
[ , [ @query_no_truncate = ] query_no_truncate ]
[ , [ @mailitem_id = ] mailitem_id ] [ OUTPUT ]
参数
[ @profile_name = ] 'profile_name'
发送邮件的配置文件的名称。profile_name 的类型为 sysname,默认值为 NULL。profile_name 必须是现有数据库邮件配置文件的名称。如果未指定 profile_name,则 sp_send_dbmail 使用当前用户的默认专用配置文件。如果该用户没有默认专用配置文件,sp_send_dbmail 会使用 msdb 数据库的默认公共配置文件。如果用户没有默认的专用配置文件,而且数据库也没有默认的公共配置文件,则必须指定 @profile_name。
[ @recipients = ] 'recipients'
要向其发送邮件的电子邮件地址列表,以分号分隔。收件人列表的类型为 varchar(max)。虽然此参数是可选参数,但是必须至少指定 @recipients、@copy_recipients 或 @blind_copy_recipients 中的一个,否则 sp_send_dbmail将返回错误。
[ @copy_recipients = ] 'copy_recipients'
要向其抄送邮件的电子邮件地址列表,以分号分隔。抄送件收件人列表的类型为 varchar(max)。虽然此参数是可选参数,但是必须至少指定 @recipients、@copy_recipients 或 @blind_copy_recipients 中的一个,否则sp_send_dbmail 将返回错误。
[ @blind_copy_recipients = ] 'blind_copy_recipients'
要向其密件抄送邮件的电子邮件地址列表,以分号分隔。密件副本收件人列表的类型为 varchar(max)。虽然此参数是可选参数,但是必须至少指定 @recipients、@copy_recipients 或@blind_copy_recipients 中的一个,否则sp_send_dbmail 将返回错误。
[ @subject = ] 'subject'
电子邮件的主题。主题的类型为 nvarchar(255)。如果未指定主题,则默认为“SQL Server 消息”。
[ @body = ] 'body'
电子邮件的正文。邮件正文的类型为 nvarchar(max),默认值为 NULL。
[ @body_format = ] 'body_format'
邮件正文的格式。该参数的类型为 varchar(20),默认值为 NULL。如果已指定,则待发邮件的标头设置会指示邮件正文具有指定格式。该参数可能包含下列值之一:
TEXT
HTML
默认为 TEXT。
[ @importance = ] 'importance'
邮件的重要性。该参数的类型为 varchar(6)。该参数可能包含下列值之一:
Low
Normal
High
默认值为 Normal。
[ @sensitivity = ] 'sensitivity'
邮件的敏感度。该参数的类型为 varchar(12)。该参数可能包含下列值之一:
Normal
Personal
Private
Confidential
默认值为 Normal。
[ @file_attachments = ] 'file_attachments',
电子邮件附件的文件名列表,以分号分隔。必须使用绝对路径指定列表中的文件。附件列表的类型为nvarchar(max)。
[ @query = ] 'query'
要执行的查询。查询结果可以作为文件附加,或包含在电子邮件的正文中。查询的类型为 nvarchar(max),并且可以包含任何有效的 Transact-SQL 语句。请注意,查询在单独的会话中执行,所以调用 sp_send_dbmail 的脚本中的局部变量不可用于查询。
[ @execute_query_database = ] 'execute_query_database'
存储过程在其中运行查询的数据库上下文。该参数的类型为 sysname,默认为当前数据库。只有在指定 @query 时,此参数才适用。
[ @attach_query_result_as_file = ] attach_query_result_as_file
指定查询结果集是否作为附件返回。attach_query_result_as_file 的数据类型为 bit,默认值为 0。
如果该值为 0,则查询结果包含在电子邮件的正文中,在 @body 参数的内容之后。如果该值为 1,则结果作为附件返回。只有在指定 @query 时,此参数才适用。
[ @query_attachment_filename = ] query_attachment_filename
指定查询结果集附件使用的文件名。query_attachment_filename 的数据类型为 nvarchar(255),默认值为 NULL。如果 attach_query_result 为 0,则忽略此参数。如果 attach_query_result 为 1 且此参数为 NULL,则数据库邮件会创建任意文件名。
[ @query_result_header = ] query_result_header
指定查询结果是否包含列标题。query_result_header 值的数据类型为 bit。如果该值为 1,则查询结果包含列标题。如果该值为 0,则查询结果不包含列标题。该参数的默认值为 1。只有在指定 @query 时,此参数才适用。
[ @query_result_width = ] query_result_width
用于设置查询结果的格式的线条宽度(字符)。query_result_width 的数据类型为 int,默认值为 256。提供的值必须介于 10 和 32767 之间。只有在指定 @query 时,此参数才适用。
[ @query_result_separator = ] 'query_result_separator'
用于分隔查询输出中的列的字符。分隔符的类型为 char(1)。默认为“ ”(空格)。
[ @exclude_query_output = ] exclude_query_output
指定是否使用电子邮件返回查询执行的输出。exclude_query_output 的数据类型为 bit,默认值为 0。当此参数为 0 时,sp_send_dbmail 存储过程的执行将在控制台上打印作为查询执行结果而返回的消息。当此参数为 1 时,sp_send_dbmail 存储过程的执行不会在控制台上打印任何查询执行消息。
[ @append_query_error = ] append_query_error
指定是否在 @query 参数指定的查询返回错误时发送电子邮件。append_query_error 的数据类型为 bit,默认值为 0。如果此参数的值为 1,则数据库邮件会发送电子邮件,并电子邮件的正文中显示查询错误消息。如果此参数的值为 0,则数据库邮件不发送电子邮件,sp_send_dbmail 在结束时会返回代码 1,指示失败。
[ @query_no_truncate = ] query_no_truncate
指定是否使用可避免截断大型可变长度数据类型(varchar(max)、nvarchar(max)、varbinary(max)、xml、text、ntext、image 和用户定义类型)的选项执行查询。设置该选项后,查询结果将不包含列标题。query_no_truncate 值的数据类型为 bit。当该值为 0 或未指定时,查询中的列将截断为 256 个字符。当该值为 1 时,不截断查询中的列。此参数的默认值为 0。
注意:
与大量数据一起使用时,@query_no_truncate 选项会占用其他资源,并可降低服务器的性能。
[ @mailitem_id = ] mailitem_id [ OUTPUT ]
可选输出参数将返回消息的 mailitem_id。mailitem_id 的类型为 int。
返回代码值
0(成功)或 1(失败)
结果集
成功时,返回消息“邮件已排队”。
备注
权限
默认情况下,msdb 数据库中的 DatabaseMailUser 数据库角色的所有成员对 sp_send_dbmail 都有执行权限。不过,如果发送邮件的用户不具有使用该请求的配置文件的权限,sp_send_dbmail 会返回错误且不发送该邮件。
示例
A. 发送电子邮件
此示例使用电子邮件地址 danw@Adventure-Works.com 向 Dan Wilson 发送电子邮件。该邮件的主题为 Automated Success Message。邮件正文包含一句话 'The stored procedure finished successfully'。
---------------------------------------------------------------------------------------------------------------
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'AdventureWorks Administrator',
@recipients = 'danw@Adventure-Works.com',
@body = 'The stored procedure finished successfully.',
@subject = 'Automated Success Message' ;
B. 发送包含查询结果的电子邮件
此示例使用电子邮件地址 danw@Adventure-Works.com 向 Dan Wilson 发送电子邮件。该邮件的主题为 Work Order Count,并执行查询以显示 DueDate 在 2004 年 4 月 30 日后的两日内的工单数。数据库邮件将该结果附加为文本文件。
---------------------------------------------------------------------------------------------------------------
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'AdventureWorks Administrator',
@recipients = 'danw@Adventure-Works.com',
@query = 'SELECT COUNT(*) FROM AdventureWorks.Production.WorkOrder
WHERE DueDate > ''2004-04-30''
AND DATEDIFF(dd, ''2004-04-30'', DueDate) < 2' ,
@subject = 'Work Order Count',
@attach_query_result_as_file = 1 ;
A. 发送 HTML 电子邮件
此示例使用电子邮件地址 danw@Adventure-Works.com 向 Dan Wilson 发送电子邮件。邮件的主题为 Work Order List,并包含一个 HTML 文档,其中列出 DueDate 在 2004 年 4 月 30 日后的二日内的工单。数据库邮件使用 HTML 格式发送该邮件。
---------------------------------------------------------------------------------------------------------------
DECLARE @tableHTML NVARCHAR(MAX) ;
SET @tableHTML =
N'<H1>Work Order Report</H1>' +
N'<table border="1">' +
N'<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
FROM AdventureWorks.Production.WorkOrder as wo
JOIN AdventureWorks.Production.Product AS p
ON wo.ProductID = p.ProductID
WHERE DueDate > '2004-04-30'
AND DATEDIFF(dd, '2004-04-30', DueDate) < 2
ORDER BY DueDate ASC,
(p.ListPrice - p.StandardCost) * wo.OrderQty DESC
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</table>' ;
EXEC msdb.dbo.sp_send_dbmail @recipients='danw@Adventure-Works.com',
@subject = 'Work Order List',
@body = @tableHTML,
@body_format = 'HTML' ;
以下为根据系统存储过程sp_helpdb修改为sp_helpdb_info的代码
---------------------------------------------------------------------------------------------------------------
USE [master]
GO
/****** Object: StoredProcedure [dbo].[sp_helpdb_info] Script Date: 09/08/2011 16:27:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[sp_helpdb_info] -- 1995/12/20 15:34 #12755
@dbname sysname = NULL -- database name
as
declare @exec_stmt nvarchar(625)
declare @showdev bit
declare @name sysname
declare @cmd nvarchar(279)
declare @low nvarchar(11)
declare @dbdesc varchar(600) /* the total description for the db */
declare @propdesc varchar(40)
set nocount on
/* Create temp table before any DMP to enure dynamic
** Since we examine the status bits in sysdatabase and turn them
** into english, we need a temporary table to build the descriptions.
*/
create table #spdbdesc
(
dbname sysname,
owner sysname null,
created nvarchar(11),
dbid smallint,
dbdesc nvarchar(600) null,
dbsize nvarchar(13) null,
cmptlevel tinyint
)
/*
** If no database name given, get 'em all.
*/
if @dbname is null
select @showdev = 0
else select @showdev = 1
/*
** See if the database exists
*/
if not exists (select * from master.dbo.sysdatabases
where (@dbname is null or name = @dbname))
begin
raiserror(15010,-1,-1,@dbname)
return (1)
end
select @low = convert(varchar(11),low) from master.dbo.spt_values
where type = N'E' and number = 1
/*
** Initialize #spdbdesc from sysdatabases
*/
insert into #spdbdesc (dbname, owner, created, dbid, cmptlevel)
select name, suser_sname(sid), convert(nvarchar(11), crdate),
dbid, cmptlevel from master.dbo.sysdatabases
where (@dbname is null or name = @dbname)
/*
** Check if you have access to database
** if have access set size and collation
*/
select @low = convert(varchar(11),low) from master.dbo.spt_values
where type = N'E' and number = 1
declare ms_crs_c1 cursor global for
select db_name (dbid) from #spdbdesc
open ms_crs_c1
fetch ms_crs_c1 into @name
while @@fetch_status >= 0
begin
if (has_dbaccess(@name) <> 1)
begin
delete #spdbdesc where current of ms_crs_c1
raiserror(15622,-1,-1, @name)
end
else
begin
/* Insert row for each database */
select @exec_stmt = 'update #spdbdesc
set dbsize = (select str(convert(dec(15),sum(size))* ' + @low + '/ 1048576,10,2)+ N'' MB'' from '
+ quotename(@name, N'[') + N'.dbo.sysfiles) WHERE current of ms_crs_c1'
execute (@exec_stmt)
end
fetch ms_crs_c1 into @name
end
deallocate ms_crs_c1
/*
** Now for each dbid in #spdbdesc, build the database status
** description.
*/
declare @curdbid smallint /* the one we're currently working on */
/*
** Set @curdbid to the first dbid.
*/
select @curdbid = min(dbid) from #spdbdesc
while @curdbid IS NOT NULL
begin
set @name = db_name(@curdbid)
-- These properties always available
SELECT @dbdesc = 'Status=' + convert(sysname,DatabasePropertyEx(@name,'Status'))
update #spdbdesc set dbdesc = @dbdesc where dbid = @curdbid
/*
** Now get the next, if any dbid.
*/
select @curdbid = min(dbid) from #spdbdesc where dbid > @curdbid
end
DECLARE @tableHTML NVARCHAR(MAX) ;
SET @tableHTML =
N'<H1>Database Info (10.7.30.3)</H1>' +
N'<table border="1">' +
N'<tr><th>dbid</th><th>name</th>' +
N'<th>db_size</th><th>owner</th><th>status</th></tr>' +
CAST ( ( SELECT td = dbid, '',
td = dbname, '',
td = dbsize, '',
td = owner, '',
td = dbdesc, ''
FROM #spdbdesc
ORDER BY dbid, dbname
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</table>' ;
EXEC msdb.dbo.sp_send_dbmail
@recipients='dba@smarttargetonline.net',
@subject = 'Database Info (10.7.30.3)',
@body = @tableHTML,
@body_format = 'HTML';
/*
** If we are looking at one database, show its file allocation.
*/
if @showdev = 1 and has_dbaccess(@dbname) = 1
begin
print N' '
select @cmd = N'use ' + quotename(@dbname) + N' exec sys.sp_helpfile'
exec (@cmd)
end
return (0) -- sp_helpdb