create procedure sp_spaceused2 --- @iobj_name varchar(90), -- The object we want size on. @updateusage varchar(5) = false -- Param. for specifying that as declare @obj_id int = object_id(@iobj_name ) -- The object we want size on. declare @obj_name nvarchar(77) -- The object name that takes up space ,@type character(2) -- The object type. ,@pages bigint -- Working variable for size calc. ,@dbname sysname ,@dbsize bigint ,@logsize bigint ,@reservedpages bigint ,@usedpages bigint ,@rowCount bigint /* ** Check to see if user wants usages updated. */ set @obj_name = OBJECT_NAME(@obj_id) 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 @obj_id IS NOT NULL begin select @dbname = parsename(OBJECT_NAME(@obj_id), 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() end /* ** Update usages if user specified to do so. */ if @updateusage = 'true' begin if @obj_name is null dbcc updateusage(0) with no_infomsgs else dbcc updateusage(0,@obj_name) with no_infomsgs print ' ' end set nocount on begin /* ** Now calculate the summary data. * Note that LOB Data and Row-overflow Data are counted as Data Pages. */ SELECT @reservedpages = SUM (reserved_page_count), @usedpages = SUM (used_page_count), @pages = SUM ( CASE WHEN (index_id < 2) THEN (in_row_data_page_count + lob_used_page_count + row_overflow_used_page_count) ELSE lob_used_page_count + row_overflow_used_page_count END ), @rowCount = SUM ( CASE WHEN (index_id < 2) THEN row_count ELSE 0 END ) FROM sys.dm_db_partition_stats WHERE object_id = @obj_id; /* ** Check if table has XML Indexes or Fulltext Indexes which use internal tables tied to this table */ IF (SELECT count(*) FROM sys.internal_tables WHERE parent_id = @obj_id AND internal_type IN (202,204,211,212,213,214,215,216)) > 0 BEGIN /* ** Now calculate the summary data. Row counts in these internal tables don't ** contribute towards row count of original table. */ SELECT @reservedpages = @reservedpages + sum(reserved_page_count), @usedpages = @usedpages + sum(used_page_count) FROM sys.dm_db_partition_stats p, sys.internal_tables it WHERE it.parent_id = @obj_id AND it.internal_type IN (202,204,211,212,213,214,215,216) AND p.object_id = it.object_id; END SELECT @obj_id as obj_id, rows = convert (char(11), @rowCount), reserved = LTRIM (STR (@reservedpages * 8, 15, 0) + ' KB'), data = LTRIM (STR (@pages * 8, 15, 0) + ' KB'), index_size = LTRIM (STR ((CASE WHEN @usedpages > @pages THEN (@usedpages - @pages) ELSE 0 END) * 8, 15, 0) + ' KB'), unused = LTRIM (STR ((CASE WHEN @reservedpages > @usedpages THEN (@reservedpages - @usedpages) ELSE 0 END) * 8, 15, 0) + ' KB') end return (0) |
No comments:
Post a Comment