sql查询表空间的使用量、剩余量及其它情况
分类:数据库| 发布:camnprbubuol| 查看:728 | 发表时间: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是:
01 | if not exists ( select * from dbo.sysobjects where id = object_id(N’[dbo].[tablespaceinfo]’) and OBJECTPROPERTY(id, N’IsUserTable’) = 1) |
02 | create table tablespaceinfo |
03 | (nameinfo varchar (50) , |
04 | rowsinfo int , reserved varchar (20) , |
06 | index_size varchar (20) , |
08 | delete from tablespaceinfo |
09 | declare @tablename varchar (255) |
10 | declare @cmdsql varchar (500) |
11 | DECLARE Info_cursor CURSOR FOR |
13 | from dbo.sysobjects o where OBJECTPROPERTY(o.id, N’IsTable’) = 1 |
14 | and o. name not like N’#%%’ order by o. name |
16 | FETCH NEXT FROM Info_cursor |
18 | WHILE @@FETCH_STATUS = 0 |
20 | if exists ( select * from dbo.sysobjects where id = object_id(@tablename) and OBJECTPROPERTY(id, N’IsUserTable’) = 1) |
22 | N’ insert into tablespaceinfo exec sp_spaceused @tbname’, |
23 | N’@tbname varchar (255)’, |
25 | FETCH NEXT FROM Info_cursor |
运行代码:
2 | sp_spaceused @updateusage = ’ TRUE ’ |
6 | order by cast ( left (ltrim(rtrim(reserved)) , len(ltrim(rtrim(reserved)))-2) as int ) desc |
运行结果: