[스크랩] Reading the SQL Server log files using TSQL

2014. 3. 6. 17:39·운영/기타
728x90
반응형

One of the issues I have is that the SQL Server Error Log is quite large and it is not always easy to view the contents with the Log File Viewer.

In a previous tip "Simple way to find errors in SQL Server error log" you discussed a method of searching the error log using VBScript. Are there any other easy ways to search and find errors in the error log files?

 

SQL Server 2005 offers an undocumented system stored procedure sp_readerrorlog. This SP allows you to read the contents of the SQL Server error log files directly from a query window and also allows you to search for certain keywords when reading the error file. This is not new to SQL Server 2005, but this tip discusses how this works for SQL Server 2005.

 

This is a sample of the stored procedure for SQL Server 2005. You will see that when this gets called it calls an extended stored procedure xp_readerrorlog.

CREATE PROC [sys].[sp_readerrorlog]( 
   @p1     INT = 0, 
   @p2     INT = NULL, 
   @p3     VARCHAR(255) = NULL, 
   @p4     VARCHAR(255) = NULL) 
AS 
BEGIN 
   IF (NOT IS_SRVROLEMEMBER(N'securityadmin') = 1) 
   BEGIN 
      RAISERROR(15003,-1,-1, N'securityadmin') 
      RETURN (1) 
   END   
   IF (@p2 IS NULL) 
       EXEC sys.xp_readerrorlog @p1 
   ELSE 
       EXEC sys.xp_readerrorlog @p1,@p2,@p3,@p4 
END

 

This procedure takes four parameters:

1. Value of error log file you want to read: 0 = current, 1 = Archive #1, 2 = Archive #2, etc...

2. Log file type: 1 or NULL = error log, 2 = SQL Agent log

3. Search string 1: String one you want to search for

4. Search string 2: String two you want to search for to further refine the results

 

If you do not pass any parameters this will return the contents of the current error log.

Here are a few examples:

 

Example 1

EXEC sp_readerrorlog 6

This statement returns all of the rows from the 6th archived error log.

 

 

Example 2

EXEC sp_readerrorlog 6, 1, '2005'

This returns just 8 rows wherever the value 2005 appears.

 

Example 3

EXEC sp_readerrorlog 6, 1, '2005', 'exec'

This returns only rows where the value '2005' and 'exec' exist.

 

xp_readerrrorlog

Even though sp_readerrolog accepts only 4 parameters, the extended stored procedure accepts at least 7 parameters.

If this extended stored procedure is called directly the parameters are as follows:

1. Value of error log file you want to read: 0 = current, 1 = Archive #1, 2 = Archive #2, etc...

2. Log file type: 1 or NULL = error log, 2 = SQL Agent log

3. Search string 1: String one you want to search for

4. Search string 2: String two you want to search for to further refine the results

5. Search from start time

6. Search to end time

7, Sort order for results: N'asc' = ascending, N'desc' = descending

EXEC master.dbo.xp_readerrorlog 6, 1, '2005', 'exec', NULL, NULL, N'desc' 
EXEC master.dbo.xp_readerrorlog 6, 1, '2005', 'exec', NULL, NULL, N'asc'

 

 

http://www.mssqltips.com/sqlservertip/1476/reading-the-sql-server-log-files-using-tsql/

 

Reading the SQL Server log files using TSQL

One of the issues I have is that the SQL Server Error Log is quite large and it is not always easy to view the contents with the Log File Viewer. In a previous tip Simple way to find errors in SQL Server error log you discussed a method of searching th

www.mssqltips.com

 

728x90
반응형
저작자표시 비영리 (새창열림)

'운영 > 기타' 카테고리의 다른 글

telegram bot 만들기  (0) 2021.04.27
세션 ID별 트랜젝션 로그 사용율  (0) 2014.10.28
RESOURCE GOVERNOR를 이용해 계정별 Resource 통제  (0) 2014.07.29
[스크랩] How to find user who ran DROP or DELETE statements on your SQL Server Objects  (0) 2013.12.05
SQL Server Log 보기(xp_readerrorlog)  (0) 2013.12.01
'운영/기타' 카테고리의 다른 글
  • 세션 ID별 트랜젝션 로그 사용율
  • RESOURCE GOVERNOR를 이용해 계정별 Resource 통제
  • [스크랩] How to find user who ran DROP or DELETE statements on your SQL Server Objects
  • SQL Server Log 보기(xp_readerrorlog)
heedol
heedol
데이터베이스 업무를 하며 경험한 내용과 장애 조치를 정리하여 동일한 문제가 발생했을 때 빠른 해결을 할 수 있도록 기록해 놓은 블로그입니다.
    반응형
    250x250
  • heedol
    iHeedol
    heedol
  • 전체
    오늘
    어제
    • 분류 전체보기 (211)
      • 개발 (9)
        • 데이터베이스 및 테이블 만들기 (1)
        • Transact-SQL (2)
        • XML 데이터 다루기 (1)
        • 인덱스와 통계 (4)
        • 프로그래밍 개체 (0)
        • 트랜젝션과 잠금 (1)
      • 운영 (104)
        • SQL Server 설치 (24)
        • SQL Server 관리 도구 (9)
        • 데이터베이스 (11)
        • 백업과 복원 (7)
        • 권한 관리 (9)
        • 암호화와 감사 (1)
        • 데이터 전송 (1)
        • 운영 업무 자동화 (6)
        • 고가용성 (8)
        • 압축 (7)
        • 성능 및 실행계획 (12)
        • 기타 (9)
      • SSIS (5)
      • Windows (6)
      • HANA (9)
        • SAP HANA 아키텍쳐 (0)
        • SAP HANA 관리 도구 (1)
        • System Administration (5)
        • 보안 및 사용자 관리 (0)
        • SAP HANA Lifecycle 관리 (0)
        • Landscape 및 네트워크 관리 (0)
        • 가용성 및 확장성 (0)
        • Data Access (1)
        • HDBSQL (1)
      • 성능개선 (2)
      • Tools (16)
      • 기타 (11)
      • 블로그 (5)
      • Archive (44)
        • ABAP (10)
        • HANADB (17)
        • DataLifeCycle (2)
        • PO (7)
  • 블로그 메뉴

    • 링크

    • 공지사항

    • 인기 글

    • 태그

      index
      MSSQL2016
      SQL2008
      mssql2012
      아카이브
      미러링
      Cluster
      log
      MSSQL
      권한
      Mirroring
      압축
      Archive
      alwayson
      failover
      로그
      xp_readerrorlog
      MS SQL
      인덱스
      Page
      sap
      Compression
      SSMS
      SAP HANA
      error
      MS SQL 2012
      SSIS
      hana
      oracle
      SQL
    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.5
    heedol
    [스크랩] Reading the SQL Server log files using TSQL
    상단으로

    티스토리툴바