--1.查询机器名 SELECT @@servername AS 机器名称 --查询已安装的SQL实例名 SELECT * FROM Sys.Servers --2.查询SQL安装路径 DECLARE @installPath VARCHAR(200) EXEC MASTER.dbo.xp_regread 'HKEY_LOCAL_MACHINE', 'SOFTWARE\Microsoft\MSSQLSERVER\setup', 'SQLPath', @installPath OUTPUT SET @installPath = @installPath + '\data\' PRINT @installPath --3.得到SQL中的各种目录 DECLARE @path VARCHAR(8000) --3.1得到当前数据库的数据文件路径 SELECT @path = RTRIM(REVERSE(FILENAME)) FROM sysfiles SELECT @path = REVERSE(SUBSTRING(@path, CHARINDEX('\', @path), 8000)) SELECT 数据文件目录 = @path --3.2得到SQL安装时设置的数据文件路径 SELECT @path = RTRIM(REVERSE(FILENAME)) FROM MASTER..sysfiles WHERE NAME = 'master' SELECT @path = REVERSE(SUBSTRING(@path, CHARINDEX('\', @path), 8000)) SELECT SQL数据文件目录 = @path --3.3根据安装路径得到默认的备份目录: 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