Search This Blog

Microsoft SQL Server: Accessing Registry using XPs 

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\\Microsoft\Windows\\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 HKLM\Software\Microsoft\Windows\CurrentVersion\Run:

image