DECLARE @name VARCHAR(500)
DECLARE @sql VARCHAR(1000)
CREATE TABLE #TableRecordCount
( TableName VARCHAR(100),
RecordCount INT
)
DECLARE dbCursor CURSOR FAST_FORWARD
FOR SELECT T.NAME FROM SYSOBJECTS T WHERE T.XTYPE = 'U' AND LEFT(T.NAME,1)<>'_' AND NOT T.NAME='Timeline' ORDER BY T.NAME
OPEN dbCursor
FETCH NEXT FROM dbCursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = 'INSERT INTO #TableRecordCount (TableName, RecordCount) '
SET @sql = @sql + 'SELECT ''' + @Name + ''' AS TableName, COUNT(*) AS CNT '
SET @sql = @sql + 'FROM [' + @name + ']'
EXEC (@sql)
FETCH NEXT FROM dbCursor INTO @name
END
CLOSE dbCursor
DEALLOCATE dbCursor
SELECT SUM(RecordCount) FROM #TableRecordCount
SELECT * FROM #TableRecordCount WHERE RecordCount>50 ORDER BY RecordCount DESC
DROP TABLE #TableRecordCount