sql server query database space available

135

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);

Comments

Submit
0 Comments

More Questions

mysql format datemysql grant all privileges to a user finding duplicate column values in table with sql
mysql format dateorder by sql mysql show all tables
get all table name in sql through phpmysql show tables in database select count of distinct values sql
import database through command linesql server substring rename table sql
selecting name that contain certain word in sqlinstall sqlite3 python select duplicates in sql
ERROR 2002 (HY000): Cant connect to local MySQL server through socket -tmp-mysql.sock (2)alter table add foreign key mysql mysql delete row
how to auto increment in sqlhow to install mysql ubuntu sql where contains
how to create a table structure from another table in mysqlselect nextval from sequence sql truncate table
install sqlite npmpostgresql check total storage GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
how to select one row in mysqlselect row from mysql where date more than 30 days sql view where tables have same column name combine results
python mysql check if database existsdrop table if exists mysql month name extract
SQL SERVER Add Identity Column to existing tableupdate using case in mysql No module named MySQLdb
sql count duplicate rowshow to install postgresql on wsl group_concat limit mysql
sort by sqlquery to list all tables in sql database SELECT User,Host FROM mysql.user;
create a view in sqlitemysql get last insert id SQL UPDATE Statement
mysql show table column full descriptionpostgressql uuid t sql get foreign key