Sql-Server
如何找到發送特定電子郵件的工作?
我的工作是向一組使用者發送一封不相關的電子郵件。
電子郵件的標題是
Staging -> [AUPAIR] arrivalDate trigger issue
。當我使用上面的標題查詢 msdb 數據庫中的作業表時,我沒有得到任何答案:
USE [msdb] GO SELECT j.job_id, s.srvname, j.name, js.step_id, js.command, j.enabled FROM dbo.sysjobs j JOIN dbo.sysjobsteps js ON js.job_id = j.job_id JOIN master.dbo.sysservers s ON s.srvid = j.originating_server_id WHERE js.command LIKE N'%Staging -> [AUPAIR] arrivalDate trigger issue%'
只有當我如下改變我的查詢時,我才能找到強盜工作:
USE [msdb] GO SELECT j.job_id, s.srvname, j.name, js.step_id, js.command, j.enabled FROM dbo.sysjobs j JOIN dbo.sysjobsteps js ON js.job_id = j.job_id JOIN master.dbo.sysservers s ON s.srvid = j.originating_server_id --WHERE js.command LIKE N'Staging -> [AUPAIR] arrivalDate trigger issue%' WHERE js.command LIKE N'%Staging -> %'
這是jobstep上的命令:
if exists ( select count(1) as total ,year(modify_date) as [Year] ,month(modify_date) as [Month] from repl_aupair r where r.arrivaldate is not null and junocore_applicationid not in( select applicationid from junocore.dbo.replicationUpdateControl where arrivaldatechanged = 1 ) and modify_Date >='2017-01-26 00:38:46.197' group by year(modify_date),month(modify_date) having year(modify_date) >= 2017 --order by [Year] desc ) begin EXEC msdb.dbo.sp_send_dbmail @profile_name = 'DBA', @recipients = 'my_emails@mycompany.com', @body = 'APIA_Repl_Sub.dbo.repl_aupair arrivalDate not null and replicationUpdateControl arrivalDateChanged still 0', @subject = 'Staging -> [AUPAIR] arrivalDate trigger issue '; end
為什麼我的第一個查詢不起作用?
結論:
我修改了我的腳本,以下兩個版本有效!
在EzLo的回答之後:
USE [msdb] GO SELECT j.job_id, s.srvname, j.name, js.step_id, js.command, j.enabled FROM dbo.sysjobs j JOIN dbo.sysjobsteps js ON js.job_id = j.job_id JOIN master.dbo.sysservers s ON s.srvid = j.originating_server_id WHERE js.command LIKE '%Staging -> \[AUPAIR] arrivalDate trigger issue%' ESCAPE '\' USE [msdb] GO SELECT j.job_id, s.srvname, j.name, js.step_id, js.command, j.enabled FROM dbo.sysjobs j JOIN dbo.sysjobsteps js ON js.job_id = j.job_id JOIN master.dbo.sysservers s ON s.srvid = j.originating_server_id WHERE js.command LIKE '%Staging -> [[]AUPAIR] arrivalDate trigger issue%'
您的第一個查詢的問題是在運算符中使用了未轉義的括號
LIKE
。使用
LIKE
運算符時,如果要查找它們,則需要轉義一些特殊字元,例如括號。括在括號中的字元將告訴 SQL 引擎在其中列出的任何字元之間查找匹配項(與正則表達式相同)。DECLARE @string VARCHAR(100) = 'ABC' SELECT 'match!' WHERE @string LIKE '%[CX]%' -- Sting must have a C or an X -- Result: match!
在您的情況下,
[AUPAIR]
在 like 中使用只會匹配 1 個字母(或者A
、U
、或)P
,而不是整個單詞。I``R
DECLARE @string VARCHAR(100) = 'Staging -> [AUPAIR] arrivalDate trigger issue' SELECT 'match!' WHERE @string LIKE '%Staging -> [AUPAIR] arrivalDate trigger issue%' -- Result: (none)
然而,這將與以下字元串匹配(只有 1 個字母!):
'Staging -> A arrivalDate trigger issue' 'Staging -> U arrivalDate trigger issue' 'Staging -> P arrivalDate trigger issue' 'Staging -> I arrivalDate trigger issue' 'Staging -> R arrivalDate trigger issue'
要正確搜尋括號,您可以使用 2 個解決方案:
- 聲明一個自定義
ESCAPE
字元。DECLARE @string VARCHAR(100) = 'Staging -> [AUPAIR] arrivalDate trigger issue' SELECT 'match!' WHERE @string LIKE '%Staging -> \[AUPAIR] arrivalDate trigger issue%' ESCAPE '\' -- Result: match!
- 用括號將開口括號括起來。
DECLARE @string VARCHAR(100) = 'Staging -> [AUPAIR] arrivalDate trigger issue' SELECT 'match!' WHERE @string LIKE '%Staging -> [[]AUPAIR] arrivalDate trigger issue%' -- Result: match!
請注意,沒有必要轉義右括號
LIKE
,因為只要在它之前有一個左括號(如果你轉義它就不會),它只會被解釋為一個特殊字元。