Accessing Registry using XPs – TSQL


You can use the undocumented extended stored procedure xp_regread to access registry entries using T-SQL.

Syntax:

xp_regread    @rootkey      = N'rootkey',
              @key          = N'key',
              @value_name   = N'value_name',
              @value        = @outputValue OUTPUT

 

For example, below code returns "C:Program Files" on my system :

DECLARE @returnValue NVARCHAR(100)
EXEC   master.dbo.xp_regread
       @rootkey      = N'HKEY_LOCAL_MACHINE',
       @key          = N'SOFTWARE\MicrosoftWindows\CurrentVersion',
       @value_name   = N'ProgramFilesDir',
       @value        = @returnValue output
SELECT @returnValue

 

xp_regread ca only be used to retrieve a single value. If you need to retrieve multiple values, you will need to use xp_instance_regenumvalues, which returns all values under a specified key.

 

For example, To retrieve a list of start-up programs from registry we will use:

 

EXEC   master..xp_instance_regenumvalues
       @rootkey = N'HKEY_LOCAL_MACHINE',
       @key     = N'SOFTWARE\Microsoft\Windows\CurrentVersion\Run'

 

this will return all entries under HKLMSoftwareMicrosoftWindowsCurrentVersionRun:

 

image 

 

Hope This Helps!

Vishal

  1. sdfsfdfds
    19.04.2012 12:40 | #1

    how can we get time zone settings if they are set in the registry

  1. No trackbacks yet.