sp_readerrorlog (Transact-SQL)

適用於:SQL Server

可讓您讀取 SQL Server 或 SQL Server Agent 錯誤記錄檔的內容,並篩選關鍵詞。

Transact-SQL 語法慣例

語法

sp_readerrorlog  
    @p1        int = 0,
    @p2        int = NULL,
    @p3        nvarchar(4000) = NULL,
    @p4        nvarchar(4000) = NULL

引數

[@p1 = ]'log_number'

這是您要檢視之記錄檔的整數 (int) 值。 目前的錯誤記錄檔值為 0,前一個是 1 (Errorlog.1),前一個是 2 (Errorlog.2),依序等等。

[@p2 = ]'product ID'

這是您要檢視其記錄之產品的整數 (int) 值。 針對 SQL Server 或 2 SQL Server Agent 使用 1。 如果未指定值,則會使用 SQL Server 產品

這是檢視錯誤記錄檔時要篩選之字串的字串值。 此值為 nvarchar(4000), 預設值為 NULL。

這是您想要篩選之其他字串的字串值,以在檢視錯誤記錄檔時進一步精簡搜尋。 此值為 nvarchar(4000), 預設值為 NULL。 這會提供第一個字串搜尋 @p3的額外篩選。

傳回碼值

沒有傳回碼

結果集

顯示所要求錯誤記錄檔的內容。 如果只使用篩選字串,則會顯示符合這些字串的行。

備註

每次啟動 SQL Server 時,目前的錯誤記錄檔會重新命名為 errorlog.1; errorlog.1變成 errorlog.2errorlog.2 會變成 errorlog.3 等等。 sp_readerrorlog可讓您讀取這些錯誤記錄檔中的任何一個,只要檔案存在。

權限

sp_readerrorlog的執行許可權僅限於系統管理員固定伺服器角色的成員

範例

下列範例會迴圈 SQL Server 錯誤記錄檔。

A. 讀取目前的 SQL Server 錯誤記錄檔

EXEC sp_readerrorlog;  

B. 顯示先前的 SQL Server Agent 錯誤記錄檔

exec sp_readerrorlog 1, 2;

C. 尋找指出資料庫正在啟動的記錄訊息

exec sp_readerrorlog 0, 1, 'database', 'start'

另請參閱