SQL 사용자 Tip & 강좌
SQLER의 사용자들이 만들어가는 SQL서버 사용자 Tip & 강좌 게시판입니다. SQL서버 개발 및 운영 관련 팁과 쿼리 노하우를 이곳에서 가장 먼저 접하실 수 있습니다. 많은 도움 되시길 바랍니다.
글 수 1,443
-- 글제목 : 디스크 남은 공간 보기
-- 작성자 : 차주언(narsas@naver.com)
-- 작성일 : 2009.11.25
-- 아래 쿼리를 사용해 커서를 돌려서 남은공간 ?? 이하면 경고를 보내게 만들어 주면 되겠지요?
-- 보안에 문제가 있어서 저 확장프로시저 블럭하기도 한다던데...
-- 다른 방법 있으면 공유 부탁드려요~
-- 더좋은 방법 있을꺼야 암암.. 이방법 맘에 좀 안들어..
CREATE TABLE #FreeSpace(
Drive char(1),
MB_Free int)
go
INSERT INTO #FreeSpace EXEC xp_Fixeddrives
SELECT * from #FreeSpace
DROP TABLE #FreeSpace

차주언
MSSQL DBA , MCT/ MCDBA
SQL프런티어 /

차주언

sp_OAMethod 를 이용하여 디스크용량보는 T-SQL입니다.
제가 만든건 아니고 블로그에서 펌 한거에요 *^^*
use master
go
if object_id('dbo.usp_fixeddrives') is not null
drop procedure dbo.usp_fixeddrives
go
create procedure dbo.usp_fixeddrives
as
/*
*********************************************************************
Author : Bouarroudj Mohamed
E-mail : mbouarroudj@sqldbtools.com
Date : March 2005
Description : xp_fixeddrives wrapper
Note : you can add handling error as following (see BOL):
exec @hr = sp_OAMethod @ObjectToken,'GetDrive', @odrive OUT, @drive
if @hr <> 0
begin
-- Obtains OLE Automation error information
exec sp_OAGetErrorInfo ...
handle error
end
*********************************************************************
*/
set nocount on
---------------------------------------------------------------------
-- Declarations
---------------------------------------------------------------------
declare
@ObjectToken int,
@drive char(1),
@odrive int,
@TotalSize varchar(20),
@MB bigint
---------------------------------------------------------------------
-- Initializations
---------------------------------------------------------------------
set @MB = 1048576
create table #Driveslist
(
Drive char(1) NOT NULL,
FreeSpaceMB int NULL,
TotalSizeMB int NULL
)
---------------------------------------------------------------------
-- Processing
---------------------------------------------------------------------
insert #Driveslist(Drive, FreeSpaceMB)
exec master.dbo.xp_fixeddrives
exec sp_OACreate 'Scripting.FileSystemObject', @ObjectToken OUT --Creates an instance of the OLE object on an instance of SQL Server.
declare DriveslistCur cursor local fast_forward
for
select Drive from #Driveslist
open DriveslistCur
fetch next from DriveslistCur into @drive
while @@FETCH_STATUS = 0
begin
-- Calls a method GetDrive
exec sp_OAMethod @ObjectToken, 'GetDrive', @odrive OUT, @drive
-- Gets a property TotalSize
exec sp_OAGetProperty @odrive,'TotalSize', @TotalSize OUT
update #Driveslist
set TotalSizeMB = @TotalSize / @MB
where drive = @drive
fetch next from DriveslistCur into @drive
end
close DriveslistCur
deallocate DriveslistCur
exec sp_OADestroy @ObjectToken
select
drive,
TotalSizeMB as 'Total(MB)',
FreeSpaceMB as 'Free(MB)',
CAST(TotalSizeMB/1024.0 as decimal(10,2)) 'Total(GB)',
CAST(FreeSpaceMB/1024.0 as decimal(10,2))'Free(GB)',
CAST((FreeSpaceMB/(TotalSizeMB * 1.0)) * 100.0 as int) as 'Free(%)'
from #Driveslist
order by drive
drop table #Driveslist
return
go
exec dbo.xp_fixeddrives
go
exec dbo.usp_fixeddrives
go