-2

这是获取物理驱动器的可用磁盘空间和总磁盘空间的代码,数据存储在临时数据库#output中。我要做的就是将表中freespace可用列中的每个值#output与“1024000”进行比较,如果该值小于这个值,那么我想将该值放在另一个表中。

我是 SQL 新手,所以我不知道该怎么做。

PS驱动器的数量可能会有所不同,所以我需要一个通用的解决方案。

declare @svrName varchar(255)
declare @sql varchar(400)

set @svrName = @@SERVERNAME
set @sql = 'powershell.exe -c "Get-WmiObject -ComputerName ' + QUOTENAME(@svrName,'''') + ' -Class Win32_Volume -Filter ''DriveType = 3'' | select name,capacity,freespace | foreach{$_.name+''|''+$_.capacity/1048576+''%''+$_.freespace/1048576+''*''}"'
--creating a temporary table
CREATE TABLE #output
(line varchar(255))
--inserting disk name, total space and free space value in to temporary table

insert #output
EXEC xp_cmdshell @sql

select rtrim(ltrim(SUBSTRING(line,1,CHARINDEX('|',line) -1))) as drivename
      ,round(cast(rtrim(ltrim(SUBSTRING(line,CHARINDEX('|',line)+1,
      (CHARINDEX('%',line) -1)-CHARINDEX('|',line)) )) as Float)/1024,0) as 'capacity(GB)'
      ,round(cast(rtrim(ltrim(SUBSTRING(line,CHARINDEX('%',line)+1,
      (CHARINDEX('*',line) -1)-CHARINDEX('%',line)) )) as Float) /1024 ,0)as 'freespace(GB)'
from #output
where line like '[A-Z][:]%'
order by drivename
4

1 回答 1

0
declare @svrName varchar(255)
declare @sql varchar(400)

set @svrName = @@SERVERNAME
set @sql = 'powershell.exe -c "Get-WmiObject -ComputerName ' + QUOTENAME(@svrName,'''') + ' -Class Win32_Volume -Filter ''DriveType = 3'' | select name,capacity,freespace | foreach{$_.name+''|''+$_.capacity/1048576+''%''+$_.freespace/1048576+''*''}"'
--creating a temporary table
CREATE TABLE #output
(line varchar(255))
--inserting disk name, total space and free space value in to temporary table

insert #output
EXEC xp_cmdshell @sql

select rtrim(ltrim(SUBSTRING(line,1,CHARINDEX('|',line) -1))) as drivename
      ,round(cast(rtrim(ltrim(SUBSTRING(line,CHARINDEX('|',line)+1,
      (CHARINDEX('%',line) -1)-CHARINDEX('|',line)) )) as Float)/1024,0) as 'capacity(GB)'
      ,round(cast(rtrim(ltrim(SUBSTRING(line,CHARINDEX('%',line)+1,
      (CHARINDEX('*',line) -1)-CHARINDEX('%',line)) )) as Float) /1024 ,0)as 'freespace(GB)'
from #output
where line like '[A-Z][:]%'
having (CHARINDEX('*',line) -1)-CHARINDEX('%',line)) )) as Float) /1024 ,0) < 20
order by drivename

在您的评论中直接回答您的问题。

当您使用函数或计算字段时,您不能将它们放在WHERE子句中,因此您必须使用HAVING. 在此解决方案中,我将您的计算字段用于freespace(GB),将其放在HAVING子句中并选择大于 20 的值。

告诉我们它是否有效,因为我不知道您是否可以将 HAVING 与这些类型的功能一起使用。如果下面的其他解决方案不起作用:

declare @svrName varchar(255)
declare @sql varchar(400)

set @svrName = @@SERVERNAME
set @sql = 'powershell.exe -c "Get-WmiObject -ComputerName ' + QUOTENAME(@svrName,'''') + ' -Class Win32_Volume -Filter ''DriveType = 3'' | select name,capacity,freespace | foreach{$_.name+''|''+$_.capacity/1048576+''%''+$_.freespace/1048576+''*''}"'
--creating a temporary table
CREATE TABLE #output
(line varchar(255))
--inserting disk name, total space and free space value in to temporary table

insert #output
EXEC xp_cmdshell @sql

select rtrim(ltrim(SUBSTRING(line,1,CHARINDEX('|',line) -1))) as drivename
      ,round(cast(rtrim(ltrim(SUBSTRING(line,CHARINDEX('|',line)+1,
      (CHARINDEX('%',line) -1)-CHARINDEX('|',line)) )) as Float)/1024,0) as 'capacity(GB)'
      ,round(cast(rtrim(ltrim(SUBSTRING(line,CHARINDEX('%',line)+1,
      (CHARINDEX('*',line) -1)-CHARINDEX('%',line)) )) as Float) /1024 ,0)as 'freespace(GB)'
into #output_2
from #output
where line like '[A-Z][:]%'
order by drivename

select *
from #output_2
where [freespace(GB)] < 20

在这种情况下,您将 #output 中的值插入另一个表 (#output_2),以便您可以WHERE直接使用该子句。

于 2012-10-01T19:10:57.360 回答