SQL Server administration and T-SQL development, Web Programming with ASP.NET and Javascript, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software Kodyaz Development Resources
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Vista, etc.


xp_regread - RegQueryValueEx() returned error 2, 'The system cannot find the file specified.'


xp_regread extended stored procedure can be used to read registry key values from the registry archive within MS SQL Server.
This t-sql undocumented extended stored procedure is not directly supported by Microsoft. So it is not recommended to use xp_regread on production systems.

It is very common if you are working with xp_regread to get the following sql exception.
The below t-sql error points that the registry entry does not exist, or the parameters you have supplied to the xp_regread statement has some improper values.

RegQueryValueEx() returned error 2, 'The system cannot find the file specified.'
Msg 22001, Level 1, State 1

Let's first start with a proper xp_regread call.

Sample T-SQL Code using xp_regread Extended Stored Procedure


DECLARE @Registry_Value VARCHAR(1000)

EXECUTE xp_regread
  'HKEY_LOCAL_MACHINE',
  'SOFTWARE\Adobe\Acrobat Reader\8.0\Installer',
  'Path',
  @Registry_Value OUTPUT

SELECT @Registry_Value

And the output registry key value is : "C:\Program Files\Adobe\Acrobat 8.0\"

regedt32 registry editor read registry key value using xp_regread sql stored procedure

DECLARE @Registry_Value VARCHAR(1000)

EXECUTE xp_regread 'HKEY_LOCAL_MACHINE',
  'SOFTWARE\Adobe\Acrobat Reader\8.0\InstallPath',
  NULL, @Registry_Value OUTPUT

EXECUTE xp_regread 'HKEY_LOCAL_MACHINE',
  'SOFTWARE\Adobe\Acrobat Reader\8.0\InstallPath',
  N'', @Registry_Value OUTPUT

EXEC xp_regread @rootkey = 'HKEY_LOCAL_MACHINE',
  @key = 'SOFTWARE\Adobe\Acrobat Reader\8.0\InstallPath',
  @value_name = N'',
  @value = @Registry_Value OUTPUT

SELECT @Registry_Value




Improper @value_name parameters passed to the xp_regread procedure


But if you execute the below script to get the registry value of the same registry entry like shown above Registry Editor screenshot, you will get some error from the sql engine.

DECLARE @Registry_Value VARCHAR(1000)

EXEC xp_regread @rootkey = 'HKEY_LOCAL_MACHINE',
  @key = 'SOFTWARE\Adobe\Acrobat Reader\8.0\InstallPath',
  @value_name='(Default)',
  @value = @Registry_Value OUTPUT

EXEC xp_regread 'HKEY_LOCAL_MACHINE',
  'SOFTWARE\Adobe\Acrobat Reader\8.0\InstallPath',
  '', @Registry_Value OUTPUT

SELECT @Registry_Value

The sql exception or the t-sql error displayed on the SQL Editor or SQL Server Management Studio will be as follows :

RegQueryValueEx() returned error 2, 'The system cannot find the file specified.'
Msg 22001, Level 1, State 1

You can find information on how you can use the undocumented t-sql stored procedure on MS SQL Server version from SQL Server 2000 to SQL2005 and SQL2008 at SQL xp_regread Extended Stored Procedure.







Related SQL Resources

SQL Server Articles

SQL Server 2012

SQL Server Tools

SQL Blog

SQL Server 2008 Blog

Certification Exams Blog

Reporting Services Blog

Analysis Services Blog

MS SQL Server Forums



Free Exam Vouchers









Copyright © 2004 - 2011 Eralper Yilmaz. All rights reserved.
Community Server, by Telligent Systems