Wednesday 20 July 2011

'TOP TABLES IN EACH DATABASE in Sql Server

PRINT '================================'
PRINT 'TOP TABLES IN EACH DATABASE     '
PRINT '================================'
SET NOCOUNT ON
DECLARE @CMD NVARCHAR(4000)
PRINT 'Successful Jobs in the Instance'
set @CMD='select CONVERT(VARCHAR(35), b.name), run_date, a.step_id  from msdb..sysjobhistory a,msdb..sysjobs b where a.job_id = b.job_id and a.run_date = convert(varchar(8),getdate()- 1,112) and run_status = 0 and b.job_id in(select job_id from msdb..sysjobs)'
EXEC SP_EXECUTESQL @CMD
GO
DECLARE NM CURSOR LOCAL STATIC FOR
SELECT NAME FROM SYSDATABASES
DECLARE @DN VARCHAR(50)
DECLARE @CMD1 NVARCHAR(4000)
DECLARE @CMD2 NVARCHAR(4000)
DECLARE @CMD3 NVARCHAR(4000)
PRINT ''
OPEN NM
FETCH NEXT FROM NM INTO @DN
WHILE @@FETCH_STATUS=0
BEGIN
PRINT 'Top Object for Database ' + @DN
SET @CMD1='select top 10  CONVERT(VARCHAR(35), name) "Tablename", (convert(bigint,sum(reserved))*8192/1024/1024) "Reserved Size(MB)", sum(rows) "Total Rows" from '+@DN+'..sysindexes   where indid in (0,1,255) group by  name order by 3 desc'
EXEC SP_EXECUTESQL @CMD1
FETCH NEXT FROM NM INTO @DN
END
CLOSE NM
DEALLOCATE NM
GO

No comments:

Post a Comment