Registry Extended Stored Procedures

Examples of how to read, write and delete registry values using SQL Server extended stored procedures.

These xp’s are not documented in SQL Server Books Online and therefore unsupported by Microsoft.


xp_regread [@rootkey=]rootkey,
[@key=]key[, [@value_name=]value_name][,
[@value=]@value OUTPUT]

1. Check for vaild key

EXEC master.dbo.xp_regread @rootkey='HKEY_LOCAL_MACHINE',   

2. Read key as result set

EXEC master.dbo.xp_regread @rootkey='HKEY_LOCAL_MACHINE',   
Value                Data  
-------------------- ------
defaultLogin         guest

3. Read key into variable

DECLARE @value varchar(25)
EXEC master.dbo.xp_regread @rootkey='HKEY_LOCAL_MACHINE',   
  @value_name='defaultLogin', @[email protected] OUTPUT
PRINT @value


xp_regwrite [@rootkey=]rootkey,
[@key=]key, [@value_name=]value_name,
[@type=]type, [@value=]value,

type can be REG_SZ for string values or REG_DWORD for integers

1. Write key and value

EXEC master.dbo.xp_regwrite @rootkey='HKEY_LOCAL_MACHINE',  
  @key='SOFTWARExp_reg', @value_name='URL', @type='REG_SZ',


xp_regdeletekey [@rootkey=]rootkey,

Beware, this will delete the whole key. Use Carefully.

1. Delete whole key

EXEC master.dbo.xp_regdeletekey                             
 @rootkey='HKEY_LOCAL_MACHINE', @key='SOFTWARExp_reg'
Msg 22001, Level 1, State 22001
RegDeleteKey() returned error 0, 
  'The operation completed successfully.'

Latest Articles