• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
fly_zj
博客园    首页    新随笔    联系   管理    订阅  订阅

扩展 xp_fixeddrives

 

xp_fixeddrvies提供磁盘分区的信息太少,如下的脚本提供磁盘分区的使用情况,运行需要管理员权限!
if not exists( select * from sys.configurations(nolock) cc 
 where cc.name='xp_cmdshell' 
 and cc.value_in_use=1)
 begin
    exec sp_configure 'show advanced options',1
    reconfigure

    exec sp_configure 'xp_cmdshell',1
    reconfigure
 end

if OBJECT_ID('tempdb..#fixeddrives_temp') is not null
drop table #fixeddrives_temp;
Go


if OBJECT_ID('tempdb..#totaldrives_temp') is not null
drop table #totaldrives_temp;
Go

create table tempdb..#fixeddrives_temp(drive char(1),freesize bigint)
create table tempdb..#totaldrives_temp(size varchar(100));
insert into  #totaldrives_temp exec xp_cmdshell 'wmic LogicalDisk get deviceid,size'
insert into  #fixeddrives_temp exec xp_fixeddrives

select    f.drive,
        t.[totalsizeMB] as [总空间MB],
        (t.[totalsizeMB]-cast(f.freesize as decimal(20,0))) as [已用空间MB],
        f.freesize as [未用空间MB],
cast(((t.[totalsizeMB]-cast(f.freesize as bigint))*100.0/t.[totalsizeMB]) as decimal(5,1)) as [已使用%],
cast((cast(f.freesize as bigint)*100.0/t.[totalsizeMB]) as decimal(5,1)) as [未使用%]
from 
(

select rtrim(left(LTRIM(size),1)) as drive,
cast(replace(replace(replace(ltrim(RIGHT(rtrim(size),LEN(size)-2)),char(9),''),char(10),''),CHAR(13),'') as bigint)/1024/1024 as [totalsizeMB]

from  #totaldrives_temp 
where size not like '%DeviceID  Size%' and LEN(size) not IN (0,1)
) AS t
inner join #fixeddrives_temp f
on t.drive=f.drive


 

posted @ 2012-05-06 12:35  fly_zj  阅读(588)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3