SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for ASP.Net, SQL Server, Reporting Services, T-SQL, Windows, AWS, SAP HANA and ABAP

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
Code

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
Code

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
Code




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
Code

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
Code

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.



SQL Server

SQL Server 2019 Installation
download SQL Server 2019
download SQL Server 2017
download SQL Server 2016
download SQL Server 2014
download SQL Server 2012
MacOS ve SQL Server 2019


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.