Accessing Registry using XPs – TSQL

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


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:




Hope This Helps!


If you like this post, do like my Facebook Page -> SqlAndMe
EMail me your questions ->
Follow me on Twitter -> @SqlAndMe

  1. sdfsfdfds
    April 19, 2012 at 12:40 pm

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

  1. July 8, 2011 at 12:25 pm
  2. October 17, 2011 at 2:35 pm
  3. January 5, 2012 at 2:48 pm
  4. January 11, 2012 at 3:53 am

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: