欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

sqlserver数据库获取数据库信息

程序员文章站 2023-11-04 19:55:22
ms sql server 获取当前数据库文件等信息,适用于多个版本: 复制代码 代码如下:select dbf.file_id as fileid  ...

ms sql server 获取当前数据库文件等信息,适用于多个版本:

复制代码 代码如下:

select dbf.file_id as fileid
    , dbf.name as [filename]
    , s.filename as filelocation
    , cast(dbf.size/128.0 as decimal(19,2)) as filesizemb
    , cast(cast(fileproperty(dbf.name, 'spaceused') as int)/128.0 as decimal(19,2)) as spaceusedmb
    , cast(dbf.size/128.0 - cast(fileproperty(dbf.name, 'spaceused') as int)/128.0 as decimal(19,2)) as availablespacemb
    , cast((dbf.size / 128.0 - (fileproperty(dbf.name, 'spaceused') / 128.0)) / (dbf.size / 128.0) * 100 as decimal(19,2)) as [%freespace]
    , dbf.growth / 128 as filegrowthmb
    , f.name as filegroupname
from sys.database_files dbf
    inner join sys.sysfiles s on dbf.name = s.name
    left join sys.filegroups f on dbf.data_space_id = f.data_space_id
order by dbf.name;

例如我们在某个database上,执行结果是:

复制代码 代码如下:

xxx_standard_data   
d:\program files\sqlserver2012\mssql11.mssqlserver\mssql\data\wolf_wanzheng.mdf    2000.00    1286.38    713.63    35.68    0    primary
xxx_standard_log   
d:\program files\sqlserver2012\mssql11.mssqlserver\mssql\data\wolf_wanzheng_2.ldf    157.19    2.47    154.72    98.43    0    null
xxx_temp2_data   
d:\program files\sqlserver2012\mssql11.mssqlserver\mssql\data\wolf_wanzheng_1.ndf    500.00    0.06    499.94    99.99    0    temp2
 xxx_temp2_log   
d:\program files\sqlserver2012\mssql11.mssqlserver\mssql\data\wolf_wanzheng_3.ldf    142.88    2.25    140.63    98.43    0    null