在SQL Server中,要获取数据库数据文件的物理位置,有三种方法:
sp_helpdb bright
name filename filegroup size
------------- ------------------------------- ---------- -----------
Bright Z:\MSSQL\data\Bright.mdf PRIMARY 10130240 KB
Bright_log Z:\MSSQL\data\Bright_log.LDF NULL 3456 KB
Bright01 Z:\MSSQL\data\Bright01.mdf PRIMARY 10919616 KB
Bright01_log Z:\MSSQL\data\Bright01_log.LDF NULL 265344 KB
Bright02 Z:\MSSQL\DATA\Bright02.ndf PRIMARY 102080 KB
select type_desc,name,physical_name,size from Bright.sys.database_files
type_desc name physical_name size
--------- ------------- -------------------------------- -----------
ROWS Bright Z:\MSSQL\data\Bright.mdf 1266280
LOG Bright_log Z:\MSSQL\data\Bright_log.LDF 432
ROWS Bright01 Z:\MSSQL\data\Bright01.mdf 1364952
LOG Bright01_log Z:\MSSQL\data\Bright01_log.LDF 33168
ROWS Bright02 Z:\MSSQL\DATA\Bright02.ndf 12760
select type_desc,name,physical_name,size from master.sys.master_files where database_id=DB_ID(Bright)
type_desc name physical_name size
--------- ------------- -------------------------------- -----------
ROWS Bright D:\MSSQL\DATA\Bright.mdf 1266280
LOG Bright_log D:\MSSQL\DATA\Bright.LDF 432
ROWS Bright01 D:\MSSQL\DATA\Bright01.mdf 1364952
LOG Bright01_log D:\MSSQL\DATA\Bright01.LDF 33168
ROWS Bright02 D:\MSSQL\DATA\Bright02.mdf 12760
对于一般的数据库,用“sp_helpdb”命令获得的文件大小更直观,其他二个命令返回的大小实际是数据块的数量,数据块的大小是8KB。
但是,如果一个数据库是用“Restore database with STANDBY”恢复的只读数据库,那只有第三种方法即从MASTER数据库中获得的文件位置才是真实的。