溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用SQL數(shù)據(jù)庫發(fā)mail

發(fā)布時間:2021-12-13 11:42:24 來源:億速云 閱讀:275 作者:小新 欄目:關(guān)系型數(shù)據(jù)庫

這篇文章主要介紹如何使用SQL數(shù)據(jù)庫發(fā)mail,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

一、先啟動郵件服務(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ā)郵件時所需要的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ā)件人顯示名稱 
@mailserver_name = 'smtp郵件服務(wù)器地址',--smtp郵件服務(wù)器地址 
@port = 25 
 官方sample
https://msdn.microsoft.com/zh-cn/library/ms182804.aspx
驗證
EXECUTE msdb.dbo.sysmail_help_account_sp ;

三、創(chuàng)建profile
--創(chuàng)建profile
exec msdb..sysmail_add_profile_sp
@profile_name = 'monitor',--profile名稱
@description='監(jiān)控郵件配置文件'
驗證
EXECUTE msdb.dbo.sysmail_help_profile_sp   ;

四、關(guān)聯(lián)account和profile
exec msdb..sysmail_add_profileaccount_sp
@profile_name = 'monitor',--profile名稱
@account_name = 'OPSDBA',--account名稱
@sequence_number = 1--account在profile中順序

驗證
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名稱
@recipients = 'rr@123.com',--收件人郵箱(有多個郵箱用分號分開)
@subject = 'SQL Server 2005 Mail 測試',--郵件標題
@body = 'Hello Mail!測試'--郵件內(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ù)庫發(fā)mail”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI