有谁知道如何返回数据库名称?我似乎无法弄清楚如何将sys.database_files
加入sys.databases
。
SELECT
a.NAME as FileName,
FG.name as FileGroup,
a.physical_name as FilePath,
a.type_desc as FileType,
CONVERT(DECIMAL(12,2),ROUND(a.size/128.000,2)) AS [Size],
CONVERT(DECIMAL(12,2),ROUND(FILEPROPERTY(a.name,'SpaceUsed')/128.000,2)) AS [Used] ,
CONVERT(DECIMAL(12,2),ROUND((a.size-FILEPROPERTY(a.name,'SpaceUsed'))/128.000,2)) AS [Free]
FROM
sys.database_files a
LEFT JOIN sys.filegroups FG ON
FG.data_space_id = a.data_space_id
答案 0 :(得分:0)
SELECT a.NAME as FileName ,b.name as FileGroup , b.physical_name as FilePath, b.type_desc as FileType,
CONVERT(DECIMAL(12,2),ROUND(b.size/128.000,2)) AS [Size],
CONVERT(DECIMAL(12,2),ROUND(FILEPROPERTY(b.name,'SpaceUsed')/128.000,2)) AS [Used] ,
CONVERT(DECIMAL(12,2),ROUND((b.size-FILEPROPERTY(b.name,'SpaceUsed'))/128.000,2)) AS [Free]
FROM Sys.databases a
INNER JOIN Sys.master_files b ON a.database_id=b.database_id
WHERE a.database_id >4
答案 1 :(得分:0)
使用sys.master_files
代替 - database_id
- 就像这样:
select
a.NAME as FileName,
FG.name as FileGroup,
a.physical_name as FilePath,
a.type_desc as FileType,
CONVERT(DECIMAL(12,2),ROUND(a.size/128.000,2)) AS [Size],
CONVERT(DECIMAL(12,2),ROUND(FILEPROPERTY(a.name,'SpaceUsed')/128.000,2)) AS [Used] ,
CONVERT(DECIMAL(12,2),ROUND((a.size-FILEPROPERTY(a.name,'SpaceUsed'))/128.000,2)) AS [Free]
from sys.master_files a
LEFT JOIN sys.filegroups FG ON
FG.data_space_id = a.data_space_id;
编辑:
要向查询添加数据库名称:
select
d.name as DatabaseName,
a.name as FileName,
FG.name as FileGroup,
a.physical_name as FilePath,
a.type_desc as FileType,
CONVERT(DECIMAL(12,2),ROUND(a.size/128.000,2)) AS [Size],
CONVERT(DECIMAL(12,2),ROUND(FILEPROPERTY(a.name,'SpaceUsed')/128.000,2)) AS [Used] ,
CONVERT(DECIMAL(12,2),ROUND((a.size-FILEPROPERTY(a.name,'SpaceUsed'))/128.000,2)) AS [Free]
from sys.databases d
left join sys.master_files a on d.database_id = a.database_id
left join sys.filegroups FG on
FG.data_space_id = a.data_space_id;