Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve Configured value SQL Error Log

enter image description here

I want to retrieve the maximum number of error log file value using SQL Query.

like image 474
John Avatar asked Jun 23 '26 09:06

John


2 Answers

From running SQL Server Profiler and tracing the calls this is what SSMS does.

declare @NumErrorLogs int
exec master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', 
                            N'Software\Microsoft\MSSQLServer\MSSQLServer',
                            N'NumErrorLogs', 
                            @NumErrorLogs OUTPUT
SELECT @NumErrorLogs
like image 194
Martin Smith Avatar answered Jun 26 '26 01:06

Martin Smith


The number of Sql error log files setting is stored in the windows registry at "[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS_ADV\MSSQLServer]\NumErrorLogs"

It can be retrieved using the following sql (from sql profiler).

 use master
    declare @HkeyLocal nvarchar(18)
    declare @MSSqlServerRegPath nvarchar(31)
    declare @InstanceRegPath sysname
    select @HkeyLocal=N'HKEY_LOCAL_MACHINE'
    select @MSSqlServerRegPath=N'SOFTWARE\Microsoft\MSSQLServer'
    select @InstanceRegPath=@MSSqlServerRegPath + N'\MSSQLServer'
    declare @NumErrorLogs int
    exec master.dbo.xp_instance_regread @HkeyLocal, @InstanceRegPath, N'NumErrorLogs', @NumErrorLogs OUTPUT
    SELECT
    ISNULL(@NumErrorLogs, -1) AS [NumberOfLogFiles]
like image 36
JimSTAT Avatar answered Jun 26 '26 00:06

JimSTAT