8

i know there is a variable, function, or stored procedure that you can use to find the path that SQL Server is installed to:

e.g.:

c:\Program Files\Microsoft SQL Server\MSSQL.7\MSSQL

or

m:\CustomInstance\MSSQL

In reality, i'm hoping to SELECT for the default backup path. But since i doubt that exists, i'll just tack \BACKUP onto the install path and call it close enough.


Update One

select filename from sysaltfiles
where name = db_name()

Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sysaltfiles'.

select filename from master.dbo.sysaltfiles
where name = db_name()

filename
---------------- 

(0 row(s) affected)
John Sansom
  • 41,005
  • 9
  • 72
  • 84
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219

4 Answers4

13

How to select the installation path

Note: xp_instance_regread doesn't read the registry key you specify, but instead converts that key path into the appropriate path for the specific SQL Server instance you're running on. In other words: xp_regread fails where xp_instance_regread succeeds.

SQL Server Installation Directory

declare @rc int, @dir nvarchar(4000) 

exec @rc = master.dbo.xp_instance_regread
      N'HKEY_LOCAL_MACHINE',
      N'Software\Microsoft\MSSQLServer\Setup',
      N'SQLPath', 
      @dir output, 'no_output'
select @dir AS InstallationDirectory

SQL Server Backup Directory

declare @rc int, @dir nvarchar(4000) 

exec @rc = master.dbo.xp_instance_regread
      N'HKEY_LOCAL_MACHINE',
      N'Software\Microsoft\MSSQLServer\MSSQLServer',
      N'BackupDirectory', 
      @dir output, 'no_output'
select @dir AS BackupDirectory

SQL Server 2000 Location Functions

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
1

Execute the following to inspect the registry in order to find the appropriate key.

Declare @Path as varchar(100);
Set @Path = NULL

Exec master..xp_regread 'HKEY_LOCAL_MACHINE', 'SOFTWARE\Microsoft\Microsoft SQL Server\70\Tools\ClientSetup', 'SQLPath', @Path OUTPUT
Select @Path as [Sql Server 7.0 path] 

Set @Path = NULL
Exec master..xp_regread 'HKEY_LOCAL_MACHINE', 'SOFTWARE\Microsoft\Microsoft SQL Server\80\Tools\ClientSetup', 'SQLPath', @Path OUTPUT
Select @Path as [Sql Server 2000 path] 

Set @Path = NULL
Exec master..xp_regread 'HKEY_LOCAL_MACHINE', 'SOFTWARE\Microsoft\Microsoft SQL Server\90\Tools\ClientSetup', 'SQLPath', @Path OUTPUT
Select @Path as [Sql Server 2005 path]

Set @Path = NULL
Exec master..xp_regread 'HKEY_LOCAL_MACHINE', 'SOFTWARE\Microsoft\Microsoft SQL Server\100\Tools\ClientSetup', 'SQLPath', @Path OUTPUT
Select @Path as [Sql Server KATMAI path]
John Sansom
  • 41,005
  • 9
  • 72
  • 84
  • That requires knowing which one of the versions of SQL Server you're currently talking to; and multiple could be on the same machine. – Ian Boyd Feb 03 '09 at 14:34
  • xp_instance_regread + SERVERPROPERTY('ProductVersion') solves that one – gbn Feb 03 '09 at 19:48
  • Turns out it doesn't return the right thing: "c:\Program Files\Microsoft SQL Server\80\Tools" vs "c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL" – Ian Boyd Feb 10 '09 at 14:57
0
CREATE FUNCTION Fn_sqlservertoolsdir()
returns NVARCHAR(4000)
AS
  BEGIN
      DECLARE @rc  INT,
              @dir NVARCHAR(4000),
              @key NVARCHAR(4000)

      SET @key = N'Software\Microsoft\Microsoft SQL Server\' + Replace(Cast(Serverproperty('ProductVersion') AS CHAR(2)), '.', '') + '0' + '\Tools\ClientSetup'

      EXEC @rc = master.dbo.Xp_regread
        N'HKEY_LOCAL_MACHINE',
        @key,
        N'Path',
        @dir output,
        'no_output'

      RETURN @dir
  END
Bridge
  • 29,818
  • 9
  • 60
  • 82
dinis
  • 1
  • 2
    you were missing "END", so I added it, formatted the code, and put it in a code block for you. – Bridge Jul 03 '12 at 10:43
0

what happens when you run this

select filename from sysaltfiles
where name = db_name()

change db_name() to 'master' if you want to see where the master database lives

SQLMenace
  • 132,095
  • 25
  • 206
  • 225