Search
 
SCRIPT & CODE EXAMPLE
 

SQL

sql server query database space available

--DISK SPACE

DROP TABLE #tmp
CREATE TABLE #tmp (COL1 VARCHAR(20),COL2 INT)
INSERT INTO #tmp
EXEC master..xp_fixeddrives
SELECT col1 AS drive, col2/1000 AS free_spce_in_gb FROM #tmp ORDER BY 2

DROP TABLE #output
create table #output (output nvarchar(255) null)
insert #output (output) EXEC master..xp_cmdshell 'PowerShell.exe -noprofile -command "get-WmiObject Win32_LogicalDisk  | Format-Table -Property DeviceID,FreeSpace"'
select LEFT(output,3) AS DRIVE, LTRIM(RTRIM(RIGHT(OUTPUT,LEN(OUTPUT)-3))) AS FREE_SPACE from #output where output LIKE '%C:%'

--SERVER SPACE

SELECT DISTINCT 
	dovs.volume_mount_point AS Drive,
	DB_NAME(dovs.database_id) DBName,
	dovs.logical_volume_name AS LogicalName,
	(CONVERT(INT,dovs.available_bytes/1048576.0) / 1000) AS FreeSpaceInGB
FROM
	sys.master_files mf
	CROSS APPLY sys.dm_os_volume_stats(mf.database_id, mf.FILE_ID) dovs
ORDER BY 
	FreeSpaceInGB ASC;

--DB SPACE
SELECT DB_NAME() AS DbName, 
    name AS FileName, 
    type_desc,
    size/128.0 AS CurrentSizeMB,  
    size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT)/128.0 AS FreeSpaceMB
FROM sys.database_files
WHERE type IN (0,1);
Comment

PREVIOUS NEXT
Code Example
Sql :: mysql order by date asc null last 
Sql :: GROUP BY With HAVING Clausel 
Sql :: invalid column name sql 
Sql :: sql max of two values 
Sql :: mysql max 
Sql :: mysql create table query 
Sql :: sql where time stamp is in between 
Sql :: how to run a function in sql 
Sql :: creating table in mysql 
Sql :: ON DUPLICATE KEY UPDATE for postgres 
Sql :: mysql biginteger size 
Sql :: insert array into mysql column 
Sql :: sort order on two columns sql 
Sql :: mysql time 
Sql :: query to find second highest salary 
Sql :: install mysql for fedora 34 
Sql :: SQL LIMIT With OFFSET Clause 
Sql :: try catch sql 
Sql :: uninstall mysql ubuntu 18.04 stackoverflow 
Sql :: osm2pgsql mac 
Sql :: const pool = mysql.createpool() 
Sql :: between 
Sql :: sqlite copy table to another table 
Sql :: sql create table 
Sql :: soql more than today 
Sql :: mysql show slave status 
Sql :: sql dcl 
Sql :: r write csv without index 
Sql :: postgresql port 5432 not open 
Sql :: replace content value from old to new sql 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =