1、select filename from master..sysfiles
2、master..xp_instance_regread N‘HKEY_LOCAL_MACHINE‘, N‘SOFTWARE\Microsoft\MSSQLServer\Setup‘, N‘SQLPath‘
--得到SQL中的各种目录
declare @path varchar(8000)
--得到当前数据库的数据文件路径
select @path=rtrim(reverse(filename)) from sysfiles
select @path=reverse(substring(@path,charindex(‘\‘,@path),8000))
select 数据文件目录=@path
--得到SQL安装时设置的数据文件路径
select @path=rtrim(reverse(filename)) from master..sysfiles where name=‘master‘
select @path=reverse(substring(@path,charindex(‘\‘,@path),8000))
select SQL数据文件目录=@path
--根据安装路径得到默认的备份目录:
select @path=rtrim(reverse(filename)) from master..sysfiles where name=‘master‘
select @path=substring(@path,charindex(‘\‘,@path)+1,8000),
@path=reverse(substring(@path,charindex(‘\‘,@path),8000))+‘BACKUP\‘
select SQL默认备份目录=@path
--读注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Setup
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