一个查看MSSQLServer数据库空间使用情况的存储过程 SpaceUsed

运行下面存储过程

然后直接使用 SpaceUsed 就可以查看了.

存储过程代码

程序代码

代码如下:

Create procedure SpaceUsed

as

begin

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)

declare @objname nvarchar(776)        -- The object we want size on.

declare @updateusage varchar(5)             -- Param. for specifying that

create table #temp1

(

表名              varchar(200) null,

行数               char(11) null,

保留空间        varchar(15) null,

数据使用空间       varchar(15) null,

索引使用空间       varchar(15) null,

未用空间          varchar(15) null

)

--select @objname='N_dep'                               -- usage info. should be updated.

select @updateusage='false'

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

*/

declare cur_table cursor for

select name from sysobjects where type='u'

Open cur_table

fetch next from cur_table into @objname

While @@FETCH_STATUS=0

begin

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

insert into #temp1

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'

Drop table #spt_space

end

fetch next from cur_table into @objname

end

Close cur_table

DEALLOCATE cur_table

Select * from #temp1 order by len(数据使用空间) desc,数据使用空间 desc,保留空间 desc

Drop table #temp1

return (0)

end

GO

(0)

相关推荐

  • sqlserver 复制表 复制数据库存储过程的方法

    在目前的工作中需要解决复制整个SqlServer数据库的问题,复制的内容包括数据库大纲.数据库中的存储过程.函数.表结构.主外键关系以及表中的所有数据等,也就是说copy版本与原数据库一模一样.经过一段时间的摸索,找到的一个比较简单的解决方案是:  (1)在复制数据库之前,先备份该数据库到文件.  (2)依据备份文件创建新的数据库,并Restore即可.  备份数据库可用如下Sql语句:  string.Format("backup database {0} to disk = '{1}';&q

  • SQLserver 数据库危险存储过程删除与恢复方法

    今天为了实现SQLServer/" target="_blank">sqlserver的复制功能,因为以前删除了很多的sqlserver的一些会导致不安全因素的扩展,导致很多功能无法用,没有办法需要重新的恢复扩展. 曾经遇过,差点抓狂,有装MSSQL的朋友,赶紧试一下,删除以下的组件.当然,前提是你要把自己的数据库搞定后再去删除,否则组件删除后很多功能不能用,为了安全,就得牺牲某些功能,当然,像我这类的,我除了把MSSQL导入后,基本上一年用不到两次,所以,我是会毫不犹

  • SQL Server中通过扩展存储过程实现数据库的远程备份与恢复

    本文通过实例解析了 SQL Server 数据库扩展存储过程,实现远程备份与恢复的方法和步骤实例说明: 环境:win2k+sqlserver 2K+查询分析器 SQL SERVER服务实例名称:mainserver 需要备份的数据库名称: msdb 本地机器名称(Client端):david 本地用户:zf 密码:123 本地域名:domain 本地提供备份需求的文件夹:e: est 第一步: 建立共享文件夹 在程序代码中调用(或者CMD窗口) net share test=e: est 或者用

  • MSSQL MySQL 数据库分页(存储过程)

    先看看单条 SQL 语句的分页 SQL 吧. 方法1: 适用于 SQL Server 2000/2005 复制代码 代码如下: SELECT TOP 页大小 * FROM table1 WHERE id NOT IN ( SELECT TOP 页大小*(页数-1) id FROM table1 ORDER BY id ) ORDER BY id 方法2: 适用于 SQL Server 2000/2005 复制代码 代码如下: SELECT TOP 页大小 * FROM table1 WHERE

  • mysql 查询数据库中的存储过程与函数的语句

    方法一: select `name` from mysql.proc where db = 'your_db_name' and `type` = 'PROCEDURE' //存储过程 select `name` from mysql.proc where db = 'your_db_name' and `type` = 'FUNCTION' //函数 方法二: show procedure status; //存储过程 SHOW PROCEDURE STATUS WHERE db='serva

  • MSSQL监控数据库的DDL操作(创建,修改,删除存储过程,创建,修改,删除表等)

    前言: 有时候,一个数据库有多个帐号,包括数据库管理员,开发人员,运维支撑人员等,可能有很多帐号都有比较大的权限,例如DDL操作权限(创建,修改,删除存储过程,创建,修改,删除表等),账户多了,管理起来就会相当麻烦,容易产生混乱,如果数据库管理员不监控数据库架构变更的话,就不知道谁对数据库架构做了啥改动(此处改动仅仅只DDL操作),尤其有时候,有些开发人员可能不按规章制度办事,绕过或忘了通知发布人员或DBA,直接去生产机做一些DDL操作,那么我们就需要对数据库架构某些更改的事件进行监控,如果能够

  • mysql 导入导出数据库以及函数、存储过程的介绍

    mysql常用导出数据命令:1.mysql导出整个数据库  mysqldump -hhostname -uusername -ppassword databasename > backupfile.sql   mysqldump -hlocalhost -uroot hqgr> hqgr.sql     (如果root用户没用密码可以不写-p,当然导出的sql文件你可以制定一个路径,未指定则存放在mysql的bin目录下) 2.mysql导出数据库一个表 mysqldump -hhostnam

  • sqlSQL数据库怎么批量为存储过程/函数授权呢?

    在工作当中遇到一个类似这样的问题:要对数据库账户的权限进行清理.设置,其中有一个用户Test,只能拥有数据库MyAssistant的DML(更新.插入.删除等)操作权限,另外拥有执行数据库存储过程.函数的权限,但是不能进行DDL操作(包括新建.修改表.存储过程等...),于是需要设置登录名Test的相关权限: 1:右键单击登录名Test的属性. 2: 在服务器角色里面选择"public"服务器角色. 3:在用户映射选项当中,选择"db_datareader".&qu

  • sql 判断数据库,表,存储过程等是否存在的代码

    代码: --库是否存在 if exists(select * from master..sysdatabases where name=N'库名') print 'exists' else print 'not exists' --------------- -- 判断要创建的表名是否存在 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[表名]') and OBJECTPROPERTY(id, N'IsU

  • SQL数据库存储过程示例解析

    什么是存储过程:存储过程可以说是一个记录集吧,它是由一些T-SQL语句组成的代码块,这些T-SQL语句代码像一个方法一样实现一些功能(对单表或多表的增删改查),然后再给这个代码块取一个名字,在用到这个功能的时候调用他就行了. 存储过程的好处: 1.由于数据库执行动作时,是先编译后执行的.然而存储过程是一个编译过的代码块,所以执行效率要比T-SQL语句高. 2.一个存储过程在程序在网络中交互时可以替代大堆的T-SQL语句,所以也能降低网络的通信量,提高通信速率. 3.通过存储过程能够使没有权限的用

随机推荐