Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Administration - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 6525

recursive sp_spaceused query

$
0
0
I have a few servers and approx. 40 DB's per server... I'm trying to determine the size of all DBs which I can get by simply using "sp_spaceused" against each database however it is a lot of work to do it manually.I can't find anything online that can loop it through and give me the results. I've tried the below:[code="sql"]DECLARE @DBName TABLE ( id INT IDENTITY(1,1), Name VARCHAR(255))DECLARE @MinID INTDECLARE @MaxID INTDECLARE @DB VARCHAR(255)declare @sql varchar(max)INSERT INTO @DBName ( Name)SELECT NameFROM [master].[dbo].[sysdatabases]SELECT @MinID = (SELECT MIN(ID) FROM @DBName)SELECT @MaxID = (SELECT MAX(ID) FROM @DBName)WHILE @MinID <= @MaxIDBEGIN SELECT @sql = ('USE ' + Name + ' GO sp_spaceused' ) FROM @DBName WHERE ID = @MinIDselect @sql SELECT @MinID = @MinID + 1 END[/code]It gives me the outcome of "USE [DBName] GO sp_spaceused" but it doesn't work as it throws an error since you only need to say "sp_spaceused" on the DB itself..any idea or does someone have a script previously used?

Viewing all articles
Browse latest Browse all 6525

Trending Articles