DECLARE @Tbl Table
(ID INT IDENTITY(1,1),name VARCHAR(100))
INSERT INTO @Tbl
(name)
VALUES
('model'),
('master')
DECLARE
@StartCnt INT = 1,
@MaxCnt INT = (SELECT COUNT (1) FROM @tbl),
@Db nVarchar(100),
@sql nVarchar(MAX)
WHILE @StartCnt <= @MaxCnt
BEGIN
SET @Db = (SELECT Name FROM @Tbl WHERE ID = @StartCnt)
PRINT @db
SET @sql = '
USE '
+@Db
+' SELECT '''
+ @DB
+''' DB, SUM(Size)/128 Used_MB, SUM(maxsize)/128 Allocated_MB
FROM sysfiles'
exec sp_executesql @sql OUTPUT
SET @StartCnt = @StartCnt+1
END