sql查询表空间的使用量、剩余量及其它情况

分类:数据库| 发布:camnprbubuol| 查看: | 发表时间:2011/2/11

在网上看到这个Oracle的sql,由此想到SqlServer的对应sql是什么?一样吗?No,

错误:

消息 208,级别 16,状态 1,第 1 行
对象名 ’sm$ts_used’ 无效。

下边是原文:(http://oldwain.itpub.net/post/6/449417)

查询表空间的使用量、剩余量:

select * from sm$ts_used;

 

select * from sm$ts_free;

比起用其它的视图写长长的一段脚本, 简便多了。

两个视图的定义:

SQL>select text from dba_views where  view_name =   ’SM$TS_FREE’;

 

TEXT

--------------------------------------------------------------------------------

select tablespace_name, sum(bytes) bytes from dba_free_space

group by tablespace_name

 

SQL>select text from dba_views where view_name = ’SM$TS_USED’;

TEXT

--------------------------------------------------------------------------------

select tablespace_name, sum(bytes) bytes from dba_segments

group by tablespace_name

据说从7.x到11g都可以用。
经过查询,SqlServer的对应sql是:
if not exists (select * from dbo.sysobjects where id = object_id(N’[dbo].[tablespaceinfo]’) and OBJECTPROPERTY(id, N’IsUserTable’) = 1)
create table tablespaceinfo --创建结果存储表
(nameinfo varchar(50) ,
rowsinfo int , reserved varchar(20) ,
datainfo varchar(20) ,
index_size varchar(20) ,
unused varchar(20) )
delete from tablespaceinfo --清空数据表
declare @tablename varchar(255) --表名称
declare @cmdsql varchar(500)
DECLARE Info_cursor CURSOR FOR
select o.name
from dbo.sysobjects o where OBJECTPROPERTY(o.id, N’IsTable’) = 1
and o.name not like N’#%%’ order by o.name
OPEN Info_cursor
FETCH NEXT FROM Info_cursor
INTO @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
if exists (select * from dbo.sysobjects where id = object_id(@tablename) and OBJECTPROPERTY(id, N’IsUserTable’) = 1)
execute sp_executesql
N’insert into tablespaceinfo exec sp_spaceused @tbname’,
N’@tbname varchar(255)’,
@tbname = @tablename
FETCH NEXT FROM Info_cursor
INTO @tablename
END
CLOSE Info_cursor
DEALLOCATE Info_cursor
GO

运行代码:

--itlearner注:显示数据库信息
sp_spaceused @updateusage = ’TRUE’
--itlearner注:显示表信息
select *
from tablespaceinfo
order by cast(left(ltrim(rtrim(reserved)) , len(ltrim(rtrim(reserved)))-2) as int) desc

 

运行结果:

365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/archives/204.html