Quantcast
Channel: < I runat="server" / >
Viewing all articles
Browse latest Browse all 10

Retrieving the DATA folder installation path from MS SQL Server using T/SQL

$
0
0

This would be useful for creating databases using SQL scripts. The SQL query below returns the installation path of the DATA folder for the installed SQL Server.

SQL Script

 

DECLARE @device_directory NVARCHAR(520)

SELECT @device_directory = 
	SUBSTRING(filename, 1, CHARINDEX(N'master.mdf', LOWER(filename)) - 1)
FROM master.dbo.sysaltfiles 
WHERE dbid = 1 AND fileid = 1

print @device_directory

Output

C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\


Viewing all articles
Browse latest Browse all 10

Trending Articles