andrew4582

fn_TableSizes

Dec 27th, 2010
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.29 KB | None | 0 0
  1. USE [DBZOrders_PROD]
  2. GO
  3.  
  4. /****** Object:  UserDefinedFunction [dbo].[fn_TableSizes]    Script Date: 12/27/2010 20:33:13 ******/
  5. IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_TableSizes]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
  6. DROP FUNCTION [dbo].[fn_TableSizes]
  7. GO
  8.  
  9. USE [DBZOrders_PROD]
  10. GO
  11.  
  12. /****** Object:  UserDefinedFunction [dbo].[fn_TableSizes]    Script Date: 12/27/2010 20:33:13 ******/
  13. SET ANSI_NULLS ON
  14. GO
  15.  
  16. SET QUOTED_IDENTIFIER ON
  17. GO
  18.  
  19. -- =============================================
  20. -- Author:      Andrew
  21. -- Create date: Monday December 27,2010 7:55 PM
  22. -- Description:
  23. -- =============================================
  24. CREATE FUNCTION [dbo].[fn_TableSizes]
  25. (
  26. )
  27. RETURNS
  28. @table_sizes TABLE
  29. (   -- Add the column definitions for the TABLE variable here
  30.     [table_name] [nvarchar](50),
  31.     [size_mb] [decimal](18, 2),
  32.     [row_count] [int]
  33. )
  34. AS
  35. BEGIN
  36.     -- Fill the table variable with the rows for your result set
  37.    
  38.     DECLARE tables_cursor CURSOR
  39.        FOR
  40.         select TOP 100 percent
  41.             t.object_id,t.TableName,t.TableSize
  42.         from
  43.             (select
  44.                   sys.objects.object_id,
  45.                   sys.objects.name as TableName,
  46.                   sum(reserved_page_count) * 8.0 / 1024 as TableSize,
  47.                   sys.objects.type
  48.             from
  49.                   sys.dm_db_partition_stats, sys.objects
  50.             where
  51.                   sys.dm_db_partition_stats.object_id = sys.objects.object_id
  52.             group by sys.objects.object_id,sys.objects.name,sys.objects.type) t
  53.  
  54.         WHERE t.type = 'u'
  55.         ORDER BY t.TableSize desc
  56.  
  57.     OPEN tables_cursor;
  58.    
  59.     DECLARE @sys_id int;
  60.     DECLARE @tablename sysname;
  61.     DECLARE @tablesize decimal(18, 2);
  62.     DECLARE @tablesize_desc varchar(20);
  63.  
  64.     FETCH NEXT FROM tables_cursor INTO @sys_id,@tablename,@tablesize;
  65.     WHILE (@@FETCH_STATUS <> -1)
  66.     BEGIN;
  67.      
  68.         SELECT @tablesize_desc = CAST(@tablesize AS varchar(20)) + ' MB';
  69.         DECLARE @Result int;
  70.        
  71.         --EXECUTE @Result = [proc_GetTableRowCount] @tablename
  72.        
  73.         INSERT INTO @table_sizes
  74.            ([table_name]
  75.            ,[size_mb]
  76.            ,[row_count])
  77.         VALUES
  78.             (@tablename,
  79.             @tablesize,
  80.             @Result)
  81.            
  82.        
  83.        FETCH NEXT FROM tables_cursor INTO @sys_id,@tablename,@tablesize;
  84.     END;
  85.     --PRINT 'The indexes on all tables have been rebuilt.';
  86.     CLOSE tables_cursor;
  87.     DEALLOCATE tables_cursor;
  88.    
  89.    
  90.     RETURN
  91. END
  92.  
  93. GO
Advertisement
Add Comment
Please, Sign In to add comment