Script to check free space for a DB file.

  • Does anyone have a good way to check the free space on a DB or more specifically a single file in a DB? I've looked at sp_spaceused but it doesn't match what I get when I look at the free space listed on the "shrink" screen.

    I want to create a script that will give me the free space (data and log files) for each database on the server.

    Thanks

    Ken

    Kenneth FisherI was once offered a wizards hat but it got in the way of my dunce cap.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Link to my Blog Post --> www.SQLStudies.com[/url]

  • try this

    declare @idint

    declare @typecharacter(2)

    declare@pagesint

    declare @dbname sysname

    declare @dbsize dec(15,0)

    declare @bytesperpagedec(15,0)

    declare @pagesperMBdec(15,0)

    create table #spt_space

    (

    objidint null,

    rowsint null,

    reserveddec(15) null,

    datadec(15) null,

    indexpdec(15) null,

    unuseddec(15) null

    )

    set nocount on

    -- Create a cursor to loop through the user tables

    declare c_tables cursor for

    selectid

    fromsysobjects

    wherextype = 'U'

    open c_tables

    fetch next from c_tables

    into @id

    while @@fetch_status = 0

    begin

    /* Code from sp_spaceused */

    insert into #spt_space (objid, reserved)

    select objid = @id, sum(reserved)

    from sysindexes

    where indid in (0, 1, 255)

    and id = @id

    select @pages = sum(dpages)

    from sysindexes

    where indid < 2

    and id = @id

    select @pages = @pages + isnull(sum(used), 0)

    from sysindexes

    where indid = 255

    and id = @id

    update #spt_space

    set data = @pages

    where objid = @id

    /* index: sum(used) where indid in (0, 1, 255) - data */

    update #spt_space

    set indexp = (select sum(used)

    from sysindexes

    where indid in (0, 1, 255)

    and id = @id)

    - data

    where objid = @id

    /* unused: sum(reserved) - sum(used) where indid in (0, 1, 255) */

    update #spt_space

    set unused = reserved

    - (select sum(used)

    from sysindexes

    where indid in (0, 1, 255)

    and id = @id)

    where objid = @id

    update #spt_space

    set rows = i.rows

    from sysindexes i

    where i.indid < 2

    and i.id = @id

    and objid = @id

    fetch next from c_tables

    into @id

    end

    select TableName = (select left(name,60) from sysobjects where id = objid),

    Rows = convert(char(11), rows),

    ReservedKB = ltrim(str(reserved * d.low / 1024.,15,0) + ' ' + 'KB'),

    DataKB = ltrim(str(data * d.low / 1024.,15,0) + ' ' + 'KB'),

    IndexSizeKB = ltrim(str(indexp * d.low / 1024.,15,0) + ' ' + 'KB'),

    UnusedKB = ltrim(str(unused * d.low / 1024.,15,0) + ' ' + 'KB')

    from #spt_space, master.dbo.spt_values d

    where d.number = 1

    and d.type = 'E'

    order by reserved desc

    drop table #spt_space

    close c_tables

    deallocate c_tables

  • This are the scripts that i used to monitor data/log files procedure are as follows:

    1. Run Create table script (create table)

    2. Run usp_data_info (create procedure)

    3. view_data = is used to view the data in the procedure usp_data_info

    ** After creating the procedure, you must run it again to gathered results or run it thru job schedule.

    NOTE: After running the procedure, All you have to do is use No. 3 (select * from view_data) when retrieving the values. I also made a script using these scripts and another script to pass the result to an excel file but the script is in testing phase. 🙂 PM me if you have any questions regarding the scripts

    "-=Still Learning=-"

    Lester Policarpio

Viewing 3 posts - 1 through 2 (of 2 total)

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