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


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',   
  @key='SOFTWAREMicrosoftMSSQLServerMSSQLServer'
KeyExist    
----------- 
1


2. Read key as result set

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


3. Read key into variable

DECLARE @value varchar(25)
EXEC master.dbo.xp_regread @rootkey='HKEY_LOCAL_MACHINE',   
  @key='SOFTWAREMicrosoftMSSQLServerMSSQLServer', 
  @value_name='defaultLogin', @value=@value OUTPUT
PRINT @value
guest


xp_regwrite


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',
  @value='http://www.swynk.com/friends/green/'


xp_regdeletekey


xp_regdeletekey [@rootkey=]rootkey,
[@key=]key


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.'

Get the Free Newsletter!

Subscribe to Cloud Insider for top news, trends & analysis

Latest Articles