XP_INSTANCE_REGREAD

  • I have a SQL2005 question:

     

    I have a query that runs fine on SQL2000

    declare @rc int,

    @dir nvarchar(5)

    exec @rc = master.dbo.xp_instance_regread

    N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\MSSQLServer\MSSQLServer\SuperSocketNetLib\Tcp',N'TcpPort', @dir output

    select @dir

    @dir returns 1433

    Under SQL2005 it returns

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

    Anyone have an idea?

    thanks

     

     


    Mike Demmitt

  • Have you checked to verify the key exists?  That error indicates a key (a.k.a. file) is not present.

  • yes, it is folder heirarchy is changed in SQL 2005.

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\MSSQLServer\SuperSocketNetLib\Tcp\IPAll\TcpPort

    It contains one folder with spaces (Microsoft SQL Server) and now master.dbo.xp_instance_regread is giving error there. I tried to put single quote, double quote, all types of braces but none of them worked.

    Anybody knows how to resolve this?

  • Try using xp_regread, such as:

    declare

    @rc int,

    @dir

    varchar(5)

    exec

    @rc = master.dbo.xp_regread

    'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\MSSQLServer\SuperSocketNetLib\Tcp\IPAll','TcpPort', @dir output

    select @dir

     

    Tim

  • Apparently, you can use xp_instance_regread with keys that have spaces if you double up the backslash on the element that has spaces, e.g.:

    exec master.dbo.xp_instance_regread 'HKEY_LOCAL_MACHINE',

    'SOFTWARE\Microsoft\\Microsoft SQL Server\FXOTDBSQL\MSSQLServer\SuperSocketNetLib\Tcp', 'TcpPort', @port OUTPUT

    Notice the doubled-up backslash before "Microsoft SQL Server".

  • Great catch on doubling the '\'.

    Thanks!

  • I'm not sure that it works in quite that way, I think the double \\ is more likely telling regread and regwrite not to perform its substitution for the current SQL version and instance name.

    The simplicity of these extended stored procedures is that you can use the generic key root and let SQL work out exactly where the registry key is located, then your code becomes portable.

    So

    “Software\Microsoft\MSSQLServer\MSSQLServer”

    becomes (in my 2008 R2 default instance)

    “Software\Microsoft\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQLServer”

  • This is the answer I was looking for, Thanks!

  • Thanks for the \\ tip. Huge help.

  • This was removed by the editor as SPAM

  • This was removed by the editor as SPAM

  • This was removed by the editor as SPAM

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply