• sqlbuddy123 (1/7/2011)


    Pls Check this ,

    Declare @path nvarchar(100)

    Declare @instance_name nvarchar(100)

    Declare @instance_name1 nvarchar(100)

    Declare @system_instance_name nvarchar(100)

    Declare @key nvarchar(1000)

    SET @instance_name = coalesce(convert(nvarchar(100), serverproperty('InstanceName')),'MSSQLSERVER');

    If @instance_name!='MSSQLSERVER'

    Set @instance_name=@instance_name

    Set @instance_name1= coalesce(convert(nvarchar(100), serverproperty('InstanceName')),'MSSQLSERVER');

    If @instance_name1!='MSSQLSERVER'

    Set @instance_name1='MSSQL$'+@instance_name1

    EXEC master.dbo.xp_regread N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\Microsoft SQL Server\Instance Names\SQL', @instance_name, @system_instance_name output;

    Set @key=N'SYSTEM\CurrentControlSet\Services\' +@instance_name1;

    EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@key,@value_name='ImagePath',@value=@path OUTPUT

    Select @path as Binaries_Path

    Thank You,

    Best Regards,

    SQLBuddy

    Great script. Resurrecting this thread from 2011 to say that if you wind up getting a NULL for the @path variable, extend it out to a nvarchar(200). We had a long named instance of SQL, and it went beyond the 100 character limit for that key value, and as a result, it returned null rather than a truncated string.

    Thought that might help someone down the road!

    Cheers,

    Steve 😀