zoukankan      html  css  js  c++  java
  • 列出数据库中各个表的磁盘空间信息

    转自http://www.sqlteam.com/item.asp?ItemID=282


    CREATE PROCEDURE sp_spaceUsedByTable
    @orderby varchar(50)='reserved_MB desc'
    AS

    /*
    NOTE: you may need to run (takes a long time) DBCC UPDATEUSAGE('database')
    You can also specify an @orderby
    Example: sp_spaceUsedByTable @orderby='rows desc'

    Louis Nguyen
    */

    set nocount on
    set ansi_warnings off
    set transaction isolation level read uncommitted

    create table #S
    (
    [name] varchar(50) null,
    [rows] varchar(50) null,
    [reserved] varchar(50) null,
    [data] varchar(50) null,
    [index_size] varchar(50) null,
    [unused] varchar(50) null
    )

    -- Create a cursor to loop through the user tables
    declare @name varchar(50)
    declare c_tables cursor for
    select name from sysobjects where xtype = 'U'

    open c_tables
    fetch next from c_tables
    into @name

    while @@fetch_status = 0 begin
    insert into #S
    exec sp_spaceUsed @name
    fetch next from c_tables into @name
    end

    close c_tables deallocate c_tables

    select [name],[rows],reserved_MB,data_MB,[index_MB],unused_MB
    into #T
    from(
    select [name]
    ,[rows]=cast([rows] as int)
    ,reserved_MB=cast(replace(reserved,'KB','') as int)/1000
    ,data_MB=cast(replace(data,'KB','') as int)/1000
    ,[index_MB]=cast(replace(index_size,'KB','') as int)/1000
    ,unused_MB=cast(replace(unused,'KB','') as int)/1000
    from #S
    ) as XX
    order by reserved_MB desc

    exec ('select * from #T order by '+@orderby)

    drop table #S
    drop table #T
  • 相关阅读:
    2020.11.6
    2020.7.15小日记
    P1536 村村通
    P1510 精卫填海
    P1020 导弹拦截
    P1164 小A点菜
    5.17练习总结
    P1135 奇怪的电梯
    P1101 单词方阵
    P1443 马的遍历
  • 原文地址:https://www.cnblogs.com/todd/p/156376.html
Copyright © 2011-2022 走看看