Table dbsize
loading with data of every database size every night.
dbsize (id, database_name, database_size, insert_date)
It looks like this:
- 1 dbname1 1000 1/1/1900
- 2 dbname2 2000 1/1/1900
- 3 dbname3 3000 1/1/1900
- 4 dbname1 1200 1/2/1900
- 5 dbname2 2200 1/2/1900
... etc
I need to get result like this
- insert_date dbname1 dbname2 dbname3
- 1/1/1900 1000 2000 3000
- 1/2/1900 1200 2200
Tried with this sql, but nothing:
declare @ColumnNames NVARCHAR(MAX) = ''
declare @SQL NVARCHAR(MAX) = ''
select @ColumnNames += QUOTENAME(database_name) + ',' from dbo.dbsize
set @ColumnNames = LEFT(@ColumnNames, LEN(@ColumnNames)-1) set @SQL =
'select * from ( select database_name ,insert_date ,id from
dbo.dbsize) as BASEDATA
PIVOT (
count(id) for database_name in ('
+@ColumnNames +
') as PivotTable'
execute sp_executesql @SQL