sp_spaceused

  • Is it possible to modify system store proc sp_spaceused. how?

    Usually when you execute sp_spaceused 'emp' the result set has name,rows,reserved,date,index_size,unused for table emp

    But I need another column to be added  where it displays the data

  • sp_spaceused is stored in MASTER database.

    Make a copy and edit as you wish.

    USE [master]

    GO

    /****** Object: StoredProcedure [dbo].[sp_spaceused] Script Date: 08/20/2007 15:55:32 ******/

    SET

    ANSI_NULLS OFF

    GO

    SET

    QUOTED_IDENTIFIER ON

    GO

    CREATE

     procedure [dbo].[sp_spaceused2] --- 1996/08/20 17:01

    @objname

    nvarchar(776) = null, -- The object we want size on.

    @updateusage varchar

    (5) = false -- Param. for specifying that

    -- usage info. should be updated.

    as

    declare

    @id int -- The object id of @objname.

    declare

    @type character(2) -- The object type.

    declare

    @pages int -- Working variable for size calc.

    declare

    @dbname sysname

    declare

    @dbsize dec(15,0)

    declare

    @logsize dec(15)

    declare

    @bytesperpage dec(15,0)

    declare

    @pagesperMB dec(15,0)

    /*Create temp tables before any DML to ensure dynamic

    ** We need to create a temp table to do the calculation.

    ** reserved: sum(reserved) where indid in (0, 1, 255)

    ** data: sum(dpages) where indid < 2 + sum(used) where indid = 255 (text)

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

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

    */

    create

    table #spt_space

    (

    rows

    int null,

    reserved dec

    (15) null,

    data dec

    (15) null,

    indexp dec

    (15) null,

    unused dec

    (15) null

    )

    /*

    ** Check to see if user wants usages updated.

    */

    if

    @updateusage is not null

    begin

    select @updateusage=lower(@updateusage)

    if @updateusage not in ('true','false')

    begin

    raiserror(15143,-1,-1,@updateusage)

    return(1)

    end

    end

    /*

    ** Check to see that the objname is local.

    */

    if

    @objname IS NOT NULL

    begin

    select @dbname = parsename(@objname, 3)

    if @dbname is not null and @dbname <> db_name()

    begin

    raiserror(15250,-1,-1)

    return (1)

    end

    if @dbname is null

    select @dbname = db_name()

    /*

    ** Try to find the object.

    */

    select @id = null

    select @id = id, @type = xtype

    from sysobjects

    where id = object_id(@objname)

    /*

    ** Does the object exist?

    */

    if @id is null

    begin

    raiserror(15009,-1,-1,@objname,@dbname)

    return (1)

    end

     

    if not exists (select * from sysindexes

    where @id = id and indid < 2)

    if @type in ('P ','D ','R ','TR','C ','RF') --data stored in sysprocedures

    begin

    raiserror(15234,-1,-1)

    return (1)

    end

    else if @type = 'V ' -- View => no physical data storage.

    begin

    raiserror(15235,-1,-1)

    return (1)

    end

    else if @type in ('PK','UQ') -- no physical data storage. --?!?! too many similar messages

    begin

    raiserror(15064,-1,-1)

    return (1)

    end

    else if @type = 'F ' -- FK => no physical data storage.

    begin

    raiserror(15275,-1,-1)

    return (1)

    end

    end

    /*

    ** Update usages if user specified to do so.

    */

    if

    @updateusage = 'true'

    begin

    if @objname is null

    dbcc updateusage(0) with no_infomsgs

    else

    dbcc updateusage(0,@objname) with no_infomsgs

    print ' '

    end

     

    set

    nocount on

    /*

    ** If @id is null, then we want summary data.

    */

    /* Space used calculated in the following way

    ** @dbsize = Pages used

    ** @bytesperpage = d.low (where d = master.dbo.spt_values) is

    ** the # of bytes per page when d.type = 'E' and

    ** d.number = 1.

    ** Size = @dbsize * d.low / (1048576 (OR 1 MB))

    */

    if

    @id is null

    begin

    select @dbsize = sum(convert(dec(15),size))

    from dbo.sysfiles

    where (status & 64 = 0)

    select @logsize = sum(convert(dec(15),size))

    from dbo.sysfiles

    where (status & 64 <> 0)

    select @bytesperpage = low

    from master.dbo.spt_values

    where number = 1

    and type = 'E'

    select @pagesperMB = 1048576 / @bytesperpage

    select database_name = db_name(),

    database_size

    =

    ltrim(str((@dbsize + @logsize) / @pagesperMB,15,2) + ' MB'),

    'unallocated space' =

    ltrim(str((@dbsize -

    (select sum(convert(dec(15),reserved))

    from sysindexes

    where indid in (0, 1, 255)

    )) / @pagesperMB,15,2)+ ' MB')

    print ' '

    /*

    ** Now calculate the summary data.

    ** reserved: sum(reserved) where indid in (0, 1, 255)

    */

    insert into #spt_space (reserved)

    select sum(convert(dec(15),reserved))

    from sysindexes

    where indid in (0, 1, 255)

    /*

    ** data: sum(dpages) where indid < 2

    ** + sum(used) where indid = 255 (text)

    */

    select @pages = sum(convert(dec(15),dpages))

    from sysindexes

    where indid < 2

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

    from sysindexes

    where indid = 255

    update #spt_space

    set data = @pages

     

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

    update #spt_space

    set indexp = (select sum(convert(dec(15),used))

    from sysindexes

    where indid in (0, 1, 255))

    - data

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

    update #spt_space

    set unused = reserved

    - (select sum(convert(dec(15),used))

    from sysindexes

    where indid in (0, 1, 255))

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

    ' ' + 'KB'),

    data

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

    ' ' + 'KB'),

    index_size

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

    ' ' + 'KB'),

    unused

    = 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'

    end

    /*

    ** We want a particular object.

    */

    else

    begin

    /*

    ** Now calculate the summary data.

    ** reserved: sum(reserved) where indid in (0, 1, 255)

    */

    insert into #spt_space (reserved)

    select sum(reserved)

    from sysindexes

    where indid in (0, 1, 255)

    and id = @id

    /*

    ** data: sum(dpages) where indid < 2

    ** + sum(used) where indid = 255 (text)

    */

    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

     

    /* 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

    /* 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)

    update #spt_space

    set rows = i.rows

    from sysindexes i

    where i.indid < 2

    and i.id = @id

    select name = object_name(@id),

    rows

    = convert(char(11), rows),

    reserved

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

    ' ' + 'KB'),

    data

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

    ' ' + 'KB'),

    index_size

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

    ' ' + 'KB'),

    unused

    = 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'

    end

    return

    (0) -- sp_spaceused


    N 56°04'39.16"
    E 12°55'05.25"

  • yeah I cehcked my master db and read that but

    I wud like to know how i can change tht for

    getting data for the table where datepart(yyyy,convert(char(10),cast(date - 36161 as datetime),101))=1995

     

  • I don't think you want to change this proc. Maybe add your own to master as sp_spaceused_2 or something.

    Be sure you keep a copy elsewhere. If you rebuilt the master db or moved to a new server, etc., the proc won't exist.

  • Peter, any way you guys can suppress the double spacing?  And, which editor are you using (I like the colors)

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Don't change anything that's owned by the system... period.

    Make a copy of sp_SpaceUsed under a different name and then make all the changes you want.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • I'm not Peter, but that looks a lot like QA to me, based on the colors.

  • I've noticed if you cut and paste directly from QA or SSMS (where everthing is single spaced) ends up double spaced here.  I usualyy use UltraEdit as an intermediare so that things don't double space, but then I lose the colors.

  • I use SSMS2008.

    The fun thing is that if I copy and paste the code to MS Word first, copy the code from MS Word and paste here, I get single line spaces.

     


    N 56°04'39.16"
    E 12°55'05.25"

  • Thanks guys.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 10 posts - 1 through 9 (of 9 total)

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