共計(jì) 2229 個(gè)字符,預(yù)計(jì)需要花費(fèi) 6 分鐘才能閱讀完成。
這篇文章主要介紹如何使用 SQL 數(shù)據(jù)庫(kù)發(fā) mail,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
一、先啟動(dòng)郵件服務(wù)
SQL SCRIPTS
exec master..sp_configure show advanced options ,1
go
reconfigure
go
exec master..sp_configure Database mail XPs ,1
go
reconfigure
go
二、創(chuàng)建發(fā)郵件時(shí)所需要的 account
exec msdb..sysmail_add_account_sp
@account_name = OPSDBA ,– 郵件賬戶 (account) 名
@description = Mail account for administrative e-mail. ,
@email_address = monitor.dataops@mail.COM ,– 發(fā)件人郵件地址
@display_name = dataops_monitor ,– 發(fā)件人顯示名稱(chēng)
@mailserver_name = smtp 郵件服務(wù)器地址 ,–smtp 郵件服務(wù)器地址
@port = 25
官方 sample
https://msdn.microsoft.com/zh-cn/library/ms182804.aspx
驗(yàn)證
EXECUTE msdb.dbo.sysmail_help_account_sp ;
三、創(chuàng)建 profile
– 創(chuàng)建 profile
exec msdb..sysmail_add_profile_sp
@profile_name = monitor ,–profile 名稱(chēng)
@description= 監(jiān)控郵件配置文件
驗(yàn)證
EXECUTE msdb.dbo.sysmail_help_profile_sp ;
四、關(guān)聯(lián) account 和 profile
exec msdb..sysmail_add_profileaccount_sp
@profile_name = monitor ,–profile 名稱(chēng)
@account_name = OPSDBA ,–account 名稱(chēng)
@sequence_number = 1–account 在 profile 中順序
驗(yàn)證
EXECUTE msdb.dbo.sysmail_help_profileaccount_sp
EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
@principal_name = dba_monitor ,
@profile_name = monitor ,
@is_default = 1 ;
五、發(fā)送郵件
exec msdb..sp_send_dbmail
@profile_name = monitor ,–profile 名稱(chēng)
@recipients = rr@123.com ,– 收件人郵箱(有多個(gè)郵箱用分號(hào)分開(kāi))
@subject = SQL Server 2005 Mail 測(cè)試 ,– 郵件標(biāo)題
@body = Hello Mail! 測(cè)試 – 郵件內(nèi)容
-=====================================
declare @sql varchar(8000);
set @sql= SELECT top 20 a.[databse_name]
,a.[table_name]
,a.[row_count]
,a.[reserved_space]
,a.[used_space] [used_space_0408]
,b.[used_space] [used_space_0401]
,a.[index_space]
,a.[unused_space]
,a.[record_time]
,convert(int,replace(a.[used_space], + + KB + + , + + + ))-convert(int,replace(b.[used_space], + + KB + + , + + + )) inc_space_KB
FROM [DBCenter].[dbo].[viewTableSpace] a join [DBCenter].[dbo].[viewTableSpace] b
on a.databse_name=b.databse_name and a.table_name=b.table_name and convert(varchar(10),a.[record_time],120) =convert(varchar(10),dateadd(day,0,getdate()),120)
and convert(varchar(10),b.[record_time],120)=convert(varchar(10),dateadd(day,-7,getdate()),120)
order by inc_space_KB desc
–print @sql
EXEC msdb.dbo.sp_send_dbmail
@profile_name = monitor ,
@recipients = yuqing.zhu@datayes.com ,
@query = @sql ,
@subject = weekly table increase top 20 ,
@attach_query_result_as_file = 1 ;
以上是“如何使用 SQL 數(shù)據(jù)庫(kù)發(fā) mail”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注丸趣 TV 行業(yè)資訊頻道!