SQL Server Agent 預設建立錯誤日誌記錄警告與錯誤。本文系統整理日誌儲存位置、存檔機制、查看方式、配置參數(sp_set_sqlagent_properties)、回收日誌、常見亂碼問題解決方案。
| 任務 | 系統預存程序 / 命令 |
|---|---|
| 查看日誌 | EXEC xp_cmdshell 'type "C:\\...\\SQLAGENT.OUT"' 或匯入資料表 |
| 重新命名日誌檔 | EXEC msdb.dbo.sp_set_sqlagent_properties @errorlog_file=N'新路徑' (重啟 Agent 生效) |
| 設定錯誤訊息接收者 | EXEC msdb.dbo.sp_set_sqlagent_properties @error_recipient=N'用戶名' (需 Messenger 服務) |
| 啟用執行追蹤記錄 | SSMS 代理屬性 → 勾選「錯誤日誌包含執行追蹤」 |
| 設定記錄層級 | @errorlogging_level:1=錯誤, 2=警告, 4=資訊(含追蹤), 可疊加 (3,5,7,6) |
| 設定編碼 | @oem_errorlog=0 (Unicode) / 1 (OEM),解決亂碼建議設 0 |
| 回收日誌 (建立新檔) | EXEC msdb.dbo.sp_cycle_agent_errorlog |
| 限制作業歷史總行數 | @jobhistory_max_rows=1000 |
| 限制單一作業歷史行數 | @jobhistory_max_rows_per_job=100 |
@oem_errorlog=0 設為 Unicode 編碼。@oem_errorlog=0。