Database Free Space Sp_spaceused

  • Simpler if you change the datatype in the temp table ;-).

    CREATE TABLE #dbs

    (

    DBNAME sysname

    , DBID INT

    , [Total Size in MB] DECIMAL(18,2)

    , [Available Space in MB] DECIMAL(18,2)

    , DriveLetter CHAR(1)

    )

    INSERT INTO

    #dbs

    (

    DBNAME

    , DBID

    , [Total Size in MB]

    , [Available Space in MB]

    , DriveLetter

    )

    EXEC sp_MSforeachdb '

    USE [?];

    SELECT

    DB_NAME() As DBNAME

    , DB_ID() AS DBID

    , SUM(size / 128.0) AS ''TotalSizeMB''

    , SUM(size / 128.0 - CAST(FILEPROPERTY(name , ''SpaceUsed'') AS int) / 128.0) AS ''AvailableSpaceMB''

    , LEFT(physical_name, 1) AS DriveLetter

    FROM

    [?].sys.database_files

    WHERE

    type_desc = ''ROWS''

    GROUP BY LEFT(physical_name, 1) '

    Select DBNAME, [Total Size in MB], [Available Space in MB]

    ,PercentFree = CONVERT(DECIMAL(18,2), [Available Space in MB] / [Total Size in MB] * 100)

    from #dbs

    drop table #dbs

  • derekr 43208 (10/26/2011)


    Cadavre (10/26/2011)


    How's this?

    DECLARE @sql AS VARCHAR(MAX)

    SELECT @sql = COALESCE(@SQL + '; ', '') + sql_command

    FROM (SELECT 'USE ' + QUOTENAME(name) +

    ' SELECT DB_NAME() As DBNAME,

    DB_ID() AS DBID, SUM(size / 128.0) AS TotalSizeMB,

    SUM(size / 128.0 - CAST(FILEPROPERTY(name , ''SpaceUsed'') AS int) / 128.0) AS AvailableSpaceMB,

    LEFT(physical_name, 1) AS DriveLetter

    FROM sys.database_files

    WHERE type_desc = ''ROWS''

    GROUP BY LEFT(physical_name, 1)' AS sql_command

    FROM sys.databases

    ) a

    EXEC(@SQL)

    Cadavre

    Unfortuanately I need to be able to put it into a Temp table

    I tried but it said that I cannot insert from a select statement that has a variable

    Something like that

    CREATE TABLE #dbs

    (

    DBNAME sysname

    , DBID INT

    , [Total Size in MB] DECIMAL(18,2)

    , [Available Space in MB] DECIMAL(18,2)

    , DriveLetter CHAR(1)

    )

    DECLARE @sql AS VARCHAR(MAX)

    SELECT @sql = COALESCE(@SQL + '; ', '') + sql_command

    FROM (SELECT 'USE ' + QUOTENAME(name) +

    ' SELECT DB_NAME() As DBNAME,

    DB_ID() AS DBID, SUM(size / 128.0) AS TotalSizeMB,

    SUM(size / 128.0 - CAST(FILEPROPERTY(name , ''SpaceUsed'') AS int) / 128.0) AS AvailableSpaceMB,

    LEFT(physical_name, 1) AS DriveLetter

    FROM sys.database_files

    WHERE type_desc = ''ROWS''

    GROUP BY LEFT(physical_name, 1)' AS sql_command

    FROM sys.databases

    ) a

    INSERT INTO

    #dbs

    (

    DBNAME

    , DBID

    , [Total Size in MB]

    , [Available Space in MB]

    , DriveLetter

    )

    EXEC(@SQL)

    SELECT * FROM #dbs

    drop table #dbs

  • Ninja's_RGR'us (10/26/2011)


    derekr 43208 (10/26/2011)


    Cadavre (10/26/2011)


    How's this?

    DECLARE @sql AS VARCHAR(MAX)

    SELECT @sql = COALESCE(@SQL + '; ', '') + sql_command

    FROM (SELECT 'USE ' + QUOTENAME(name) +

    ' SELECT DB_NAME() As DBNAME,

    DB_ID() AS DBID, SUM(size / 128.0) AS TotalSizeMB,

    SUM(size / 128.0 - CAST(FILEPROPERTY(name , ''SpaceUsed'') AS int) / 128.0) AS AvailableSpaceMB,

    LEFT(physical_name, 1) AS DriveLetter

    FROM sys.database_files

    WHERE type_desc = ''ROWS''

    GROUP BY LEFT(physical_name, 1)' AS sql_command

    FROM sys.databases

    ) a

    EXEC(@SQL)

    Cadavre

    Unfortuanately I need to be able to put it into a Temp table

    I tried but it said that I cannot insert from a select statement that has a variable

    Something like that

    CREATE TABLE #dbs

    (

    DBNAME sysname

    , DBID INT

    , [Total Size in MB] DECIMAL(18,2)

    , [Available Space in MB] DECIMAL(18,2)

    , DriveLetter CHAR(1)

    )

    DECLARE @sql AS VARCHAR(MAX)

    SELECT @sql = COALESCE(@SQL + '; ', '') + sql_command

    FROM (SELECT 'USE ' + QUOTENAME(name) +

    ' SELECT DB_NAME() As DBNAME,

    DB_ID() AS DBID, SUM(size / 128.0) AS TotalSizeMB,

    SUM(size / 128.0 - CAST(FILEPROPERTY(name , ''SpaceUsed'') AS int) / 128.0) AS AvailableSpaceMB,

    LEFT(physical_name, 1) AS DriveLetter

    FROM sys.database_files

    WHERE type_desc = ''ROWS''

    GROUP BY LEFT(physical_name, 1)' AS sql_command

    FROM sys.databases

    ) a

    INSERT INTO

    #dbs

    (

    DBNAME

    , DBID

    , [Total Size in MB]

    , [Available Space in MB]

    , DriveLetter

    )

    EXEC(@SQL)

    SELECT * FROM #dbs

    drop table #dbs

    Great stuff

    Thanks Ninja

    Appreciate it.....

  • And Thanks Cadavre

  • derekr 43208 (10/26/2011)


    Cadavre

    Unfortuanately I need to be able to put it into a Temp table

    I tried but it said that I cannot insert from a select statement that has a variable

    Something like that

    Sorry, was in a meeting so didn't catch your post.

    Ninja's_RGR'us (10/26/2011)


    CREATE TABLE #dbs

    (

    DBNAME sysname

    , DBID INT

    , [Total Size in MB] DECIMAL(18,2)

    , [Available Space in MB] DECIMAL(18,2)

    , DriveLetter CHAR(1)

    )

    DECLARE @sql AS VARCHAR(MAX)

    SELECT @sql = COALESCE(@SQL + '; ', '') + sql_command

    FROM (SELECT 'USE ' + QUOTENAME(name) +

    ' SELECT DB_NAME() As DBNAME,

    DB_ID() AS DBID, SUM(size / 128.0) AS TotalSizeMB,

    SUM(size / 128.0 - CAST(FILEPROPERTY(name , ''SpaceUsed'') AS int) / 128.0) AS AvailableSpaceMB,

    LEFT(physical_name, 1) AS DriveLetter

    FROM sys.database_files

    WHERE type_desc = ''ROWS''

    GROUP BY LEFT(physical_name, 1)' AS sql_command

    FROM sys.databases

    ) a

    INSERT INTO

    #dbs

    (

    DBNAME

    , DBID

    , [Total Size in MB]

    , [Available Space in MB]

    , DriveLetter

    )

    EXEC(@SQL)

    SELECT * FROM #dbs

    drop table #dbs

    Thanks for covering 🙂


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • NP.

    Was what so important in that meeting that could make this unbeknownst guy wait for his answer! 😀

    You should talk to the authorities about that!

  • Ninja's_RGR'us (10/26/2011)


    NP.

    Was what so important in that meeting that could make this unbeknownst guy wait for his answer! 😀

    You should talk to the authorities about that!

    heh. I'll attend by WebEx next time, promise 😛


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Cadavre (10/26/2011)


    Ninja's_RGR'us (10/26/2011)


    NP.

    Was what so important in that meeting that could make this unbeknownst guy wait for his answer! 😀

    You should talk to the authorities about that!

    heh. I'll attend by WebEx next time, promise 😛

    You migh include that in the "the good job" thread :w00t:.

Viewing 8 posts - 31 through 37 (of 37 total)

You must be logged in to reply to this topic. Login to reply