Ongoing service disruptions
For the most recent update on ongoing service disruptions affecting the AWS Middle East (UAE) Region (ME-CENTRAL-1), refer to the AWS Health Dashboard. For information on AWS Service migration, see How do I migrate my services to another region?
如何在 Amazon RDS for SQL Server 執行個體中引發和擷取錯誤事件?
我想要引發並擷取 Amazon Relational Database Service (Amazon RDS) for SQL Server 資料庫執行個體中的錯誤事件。我還希望在發生錯誤事件時收到通知。
簡短描述
SQL Server 使用錯誤處理來解決 T-SQL 程式碼中的物件存在錯誤和執行時期錯誤。若要對此類錯誤進行疑難排解,請使用 TRY...CATCH 建構模組。然後使用 RAISERROR 命令產生自訂錯誤並擲回例外狀況。若要監控 SQL Server 錯誤日誌並接收相關通知,請使用 Amazon CloudWatch Logs。
解決方法
使用 TRY...CATCH 建構模組和 RAISERROR 陳述式
請完成下列步驟:
-
登入 SQL Server Management Studio (SSMS)。
-
使用以下 TRY...CATCH 建構模組定義用於錯誤測試的程式碼區塊:
BEGIN TRY --code to try END TRY BEGIN CATCH --code to run if an error occurs --is generated in try END CATCH**注意:**statement_block 會監控 BEGIN TRY 與 END TRY 之間的程式碼是否有執行時期錯誤。當區塊中出現錯誤時,錯誤會傳送到 CATCH 工作階段。然後,根據 CATCH 區塊中的程式碼,該陳述式會執行動作。根據問題,您可以修復錯誤、報告錯誤或將錯誤記錄到 SQL Server 錯誤日誌中。
-
建立一個自訂訊息,當發生 SQL Server 錯誤時引發該訊息。將以下 RAISERROR 陳述式新增至您想要監控的預存程序或 SQL Server 中:
RAISERROR ( { msg_id | msg_str | @local_variable } { , severity, state } [ , argument [ , ...n ] ] ) [ WITH option [ , ...n ] ]TRY 和 CATCH 建構模組和 RAISERROR 命令範例:
BEGIN TRY SELECT 1/0 END TRY BEGIN CATCH DECLARE @Var VARCHAR(100) SELECT ERROR_MESSAGE() SELECT @Var = ERROR_MESSAGE() RAISERROR(@Var, 16,1) WITH LOG END CATCH因此,它會在 SQL Server 日誌中引發以下錯誤訊息:
Error: 50000, Severity: 16, State: 1. Divide by zero error encountered.
監控 SQL Server 錯誤日誌並傳送通知
在作業步驟中新增指令碼以監視 SQL Server 代理程式作業,並在 SQL Server 錯誤日誌中引發錯誤。然後您可以使用錯誤日誌傳送通知。
若要編輯 SQL Server 代理程式作業,請完成下列步驟:
-
登入 SSMS。
-
對於類型,請選擇 T-SQL。
-
輸入資料庫名稱,然後在 command 命令區段新增以下 T-SQL:
DECLARE @name NVARCHAR(128) select @name = name from msdb.dbo.sysjobs where job_id = $(ESCAPE_SQUOTE(JOBID)); -- Temporary table to store the data of the datafile with low free storage DECLARE @jb TABLE ([step_id] int, [step_name] NVARCHAR(128), [message] NVARCHAR(4000), [run_status] int); insert into @jb select hist.step_id, hist.step_name, hist.message, hist.run_status from msdb.dbo.sysjobhistory hist inner join (select a.job_id , convert(varchar(50),max(a.run_requested_date),112) as run_date , replace(convert(varchar(50),max(a.run_requested_date),108), ':', '') as run_time from msdb.dbo.sysjobs j inner join msdb.dbo.sysjobactivity a on j.job_id = a.job_id where j.name = @name and a.run_requested_date is not null group by a.job_id) ja on hist.job_id = ja.job_id and hist.run_date = ja.run_date and hist.run_time >= ja.run_time order by hist.step_id declare @error int declare @errormsg nvarchar(4000) select @error = count(run_status) from @jb where run_status != 0 if @error > 0 begin set @errormsg='Automatic message from RDS for SQL Server Agent - Job: "' + @name + '" succeed' RAISERROR(@errormsg, -1,1) WITH LOG end else begin set @errormsg='Automatic message from RDS for SQL Server Agent - Job: "' + @name + '" failed' RAISERROR(@errormsg, 16,1) WITH LOG end -
設定上述作業步驟,以便在成功時和失敗時前往下一個作業步驟。如需詳細資訊,請參閱 Microsoft 網站上的設定作業步驟成功或失敗流程。成功時和失敗時作業步驟將執行上述程式碼,以檢查執行是否成功。然後,代理程式作業會在 SQL Server 錯誤日誌中發出訊息,顯示作業是成功還是失敗。
-
執行下列程序以確認 SQL Server 作業正確運作,並更新 SQL Server 錯誤日誌中失敗作業的詳細資訊:
EXEC rdsadmin.dbo.rds_read_error_log @index = 0, @type = 1;如需詳細資訊,請參閱查看錯誤和代理程式日誌。
以下是錯誤日誌中更新的作業詳細資訊的範例:Automatic message from RDS for SQL Server Agent - Job: "jobtest-new" succeed Error: 50000, Severity: 16, State: 1. Automatic message from RDS for SQL Server Agent - Job: "jobtest-new" failed
在 CloudWatch Logs 中設定通知
若要在 CloudWatch 中設定通知,請參閱將 SQL Server 日誌發佈到 Amazon CloudWatch Logs。
將 SQL Server 日誌發佈到 CloudWatch Logs 後,您可以建立指標篩選器來協助您搜尋日誌。指標篩選器會將日誌資料轉換為數值 CloudWatch 指標 (您可以為其設定警示)。如需詳細資訊,請參閱如何接收與 CloudWatch 篩選模式相符的 Amazon RDS for SQL Server 錯誤和代理程式日誌事件相關 SNS 通知?
相關內容
- 已提問 3 年前
- 已提問 2 年前
