rss
twitter
facebook

Home

You Can view how to size your tables

DECLARE
@max INT,
@min INT,
@owner NVARCHAR(256),
@table_name NVARCHAR(256),
@sql NVARCHAR(4000)

DECLARE @table TABLE(
ident INT IDENTITY(1,1) PRIMARY KEY,
owner_name NVARCHAR(256),
table_name NVARCHAR(256))

IF (SELECT OBJECT_ID('tempdb..#results')) IS NOT NULL
BEGIN
DROP TABLE #results
END

CREATE TABLE #results(
ident INT IDENTITY(1,1) PRIMARY KEY, --Will be used to update the owner.
table_name NVARCHAR(256),
owner_name NVARCHAR(256),
table_rows INT,
reserved_space NVARCHAR(55),
data_space NVARCHAR(55),
index_space NVARCHAR(55),
unused_space NVARCHAR(55))

INSERT @table(owner_name, table_name)
SELECT
su.name,
so.name
FROM
sysobjects so
INNER JOIN sysusers su ON so.uid = su.uid
WHERE
so.xtype = 'U'

SELECT
@min = 1,
@max = (SELECT MAX(ident) FROM @table)

WHILE @min <= @max
BEGIN

SELECT
@owner = owner_name,
@table_name = table_name
FROM
@table
WHERE
ident = @min

SELECT @sql = 'EXEC sp_spaceused ''[' + @owner + '].[' + @table_name + ']'''
INSERT #results(table_name, table_rows, reserved_space, data_space, index_space, unused_space)
EXEC (@sql)

UPDATE #results
SET owner_name = @owner
WHERE ident = (SELECT MAX(ident) FROM #results)

SELECT @min = @min + 1
END

SELECT * FROM #results

0 comentários:

 
Powered by Blogger