Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

sp_configure 'show advanced options' ,1

Go
reconfigure
Go
sp_configure 'xp_cmdshell' ,1
Go
reconfigure

DECLARE @Drive TINYINT,


@SQL VARCHAR(100)
SET @Drive = 97
DECLARE @Drives TABLE
(
Drive CHAR(1),
Info VARCHAR(80)
)
WHILE @Drive <= 122
BEGIN
SET @SQL = 'EXEC XP_CMDSHELL ''fsutil volume diskfree ' + CHAR(@Drive) + ':'''

INSERT @Drives
(
Info)
EXEC (@SQL)
UPDATE @Drives
SET Drive = CHAR(@Drive)
WHERE Drive IS NULL
SET @Drive = @Drive + 1
END

CREATE TABLE #DiskSpace


( DriveLetter char(1),
TotalSize decimal(20,0),
AvailableSpace decimal(20,0),

) INSERT INTO #DiskSpace (DriveLetter, TotalSize, AvailableSpace)


SELECT Drive,
SUM(CASE WHEN Info LIKE 'Total # of bytes%' THEN CAST(REPLACE(SUBSTRING(Info, 32,
48), CHAR(13), '') AS BIGINT) ELSE CAST(0 AS BIGINT) END) AS TotalBytes,
SUM(CASE WHEN Info LIKE 'Total # of free bytes%' THEN CAST(REPLACE(SUBSTRING(Info,
32, 48), CHAR(13), '') AS BIGINT) ELSE CAST(0 AS BIGINT) END) AS FreeBytes
--SUM(CASE WHEN Info LIKE 'Total # of avail free bytes : %' THEN
CAST(REPLACE(SUBSTRING(Info, 32, 48), CHAR(13), '') AS BIGINT) ELSE CAST(0 AS
BIGINT) END) AS AvailFreeBytes
FROM (
SELECT Drive
, Info
FROM @Drives
WHERE Info LIKE 'Total # of %'
) AS d
GROUP BY Drive
ORDER BY Drive

----select DriveLetter , (((TotalSize / 1024)/1024)/1024) As [TotalSize in GB],


----AvailableSpace/ (1024*1024*1024) as [AvailableSpace in GB] from #DiskSpace
order by DriveLetter
----select sum (TotalSize)/ (1024*1024*1024)as [Total Size of disk in GB] from
#DiskSpace
/*****Result in roud off ********/
select UPPER(DriveLetter) as [Disk Drive] , CAST(ROUND((((TotalSize /
1024)/1024)/1024) , 2) AS FLOAT) As [TotalSize in GB],
CAST(ROUND(AvailableSpace/ (1024*1024*1024) , 2) AS FLOAT) as [AvailableSpace in
GB] from #DiskSpace order by DriveLetter

drop table #DiskSpace

CREATE TABLE #temp ([Server_Name] VARCHAR (50),[Total_Physical_Memory_In_GB]


VARCHAR (50),[Available_Physical_Memory_In_GB] VARCHAR (50),
[Min_Server_Memory_Allocated_For_SQLServer] SQL_VARIANT,
[Max_Server_Memory_Allocated_For_SQLServer] SQL_VARIANT)
GO
-- To get the total physical memory installed on SQL Server
INSERT INTO #temp ([Server_Name],[Total_Physical_Memory_In_GB],
[Available_Physical_Memory_In_GB],[Min_Server_Memory_Allocated_For_SQLServer] ,
[Max_Server_Memory_Allocated_For_SQLServer] ) SELECT @@SERVERNAME,A.
[total_physical_memory_kb] / 1024 /1024
,A.[available_page_file_kb] / 1024 / 1024 ,B.value,B2.value
--,[total_page_file_kb] / 1024 / 1024 AS [Total_Page_File_In_GB]
--,[available_page_file_kb] / 1024 / 1024 AS [Available_Page_File_GB]
--,[kernel_paged_pool_kb] / 1024 / 1024 AS [Kernel_Paged_Pool_MG]
--,[kernel_nonpaged_pool_kb] / 1024 / 1024 AS [Kernel_Nonpaged_Pool_GB]
FROM [master].[sys].[dm_os_sys_memory] AS A , [master].[sys].[configurations] AS
B , [master].[sys].[configurations] AS B2
WHERE B.NAME = ('Min server memory (MB)') AND B2.NAME = 'Max server memory (MB)'
GO
SELECT * FROM #temp
GO
DROP TABLE #temp

You might also like