if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[usp_tablespace]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[usp_tablespace]GOCREATE procedure usp_tablespace @database varchar(255)ASbeginset nocount ondeclare @row int, @maxrow int, @tablename varchar(255)declare @name varchar(255), @rows int, @dataspaceused int, @indexspaceused intdeclare @totaldataspace int, @totalindexspace intdeclare @sql nvarchar(4000)set @row = 1create table #space(rowid int identity,name varchar(255),rows integer,dataspaceused integer,indexspaceused integer)create table #space2(rows integer,dataspaceused integer,indexspaceused integer)SET @SQL = 'USE ' + @database + ' insert into #space(name) exec sp_msforeachtable ''select ''''?'''''''exec sp_executesql @sqlselect @maxrow = max(rowid)from #spacewhile @row <= @maxrowbegin select @tablename = name from #space where rowid = @row set @sql = 'use ' + @database + ' insert #space2 (rows,dataspaceused,indexspaceused) exec sp_mstablespace @name = ''' + @tablename + '''' exec sp_executesql @sql select @name = @tablename, @rows = rows, @dataspaceused = dataspaceused, @indexspaceused = indexspaceused from #space2 update #space set rows = ISNULL(@rows,0), dataspaceused = ISNULL(@dataspaceused,0), indexspaceused = ISNULL(@indexspaceused,0) where name = @name delete #space2 set @row = @row + 1endselect sum(dataspaceused) as 'Total User DataSpace', sum(indexspaceused) as 'Total User IndexSpace'from #spaceselect @totaldataspace = sum(dataspaceused)from #spaceselect @totalindexspace = sum(indexspaceused)from #spaceselect name,rows,dataspaceused,indexspaceused, convert(decimal(5,2),convert(float,dataspaceused)/convert(float,@totaldataspace)) as 'percent dataspace', convert(decimal(5,2),convert(float,indexspaceused)/convert(float,@totalindexspace)) as 'percent indexspace'from #spaceorder by dataspaceused descdrop table #spacedrop table #space2RETURNendGO
JayEdited by - Jay99 on 04/05/2002 09:30:38