4

I'm creating a database with a filegroup and a file:

CREATE DATABASE SuperDb;

ALTER DATABASE SuperDb
ADD FILEGROUP FileStreamGroup CONTAINS FILESTREAM

ALTER DATABASE SuperDb
ADD FILE
(
    NAME = Blobbiez,
    FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\BLOBZ'
)
TO FILEGROUP FileStreamGroup;

This script does its job, but I want to use relative path for a file, because it will be executed in different machines. The file (actually, the folder) must be located just near the database file. How to achieve it?

astef
  • 8,575
  • 4
  • 56
  • 95

1 Answers1

1
declare @path varchar(200) 
exec master.dbo.xp_regread 
'HKEY_LOCAL_MACHINE', 
'SOFTWARE\Microsoft\MSSQLSERVER\setup', 
'SQLPath',@path output 
set @path = @path + '\data\' 
print @path
sunysen
  • 2,265
  • 1
  • 12
  • 13
  • Thank you, it works. But I still beleive the "native" way exists – astef Nov 18 '14 at 08:21
  • 1
    that doesn't give you the default data directory of the SQL instance but the install ROOT with can be different!. see here for the correct check https://stackoverflow.com/questions/1883071/how-do-i-find-the-data-directory-for-a-sql-server-instance – Tilo Feb 16 '15 at 18:55