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?

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, it works. But I still beleive the "native" way exists
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 stackoverflow.com/questions/1883071/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.