|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, October 30, 2012 7:36 PM
Points: 148,
Visits: 69
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, May 03, 2013 9:39 AM
Points: 9,
Visits: 20
|
|
I've always found cursors a really slow way of doing pretty much anything in SQL, to say nothing of being a horror to code if it's big enough.
There could be a fourth way. Create a temporary table containing the name and object_id values of every user table in the database. Then create a loop to do the following:
a. Select the name of the table object with the lowest object_id value; b. Execute a string of dynamic SQL to count the number of rows in the table bearing the name that you retrieved in step 1; c. Delete the record in the temporary table that you've just been looking at; d. Repeat until the temporary table is empty;
You could refine it further by writing the result of the count into a new table (temporary or otherwise). Or even by adding an additional column to the original temporary table and then updating that each time. The only difference is that you wouldn't delete the record once you'd finished with it, merely select the minimum object_id value as long as it was higher than the object_id value you'd just finished with.
Just a thought
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Tuesday, May 07, 2013 1:03 PM
Points: 485,
Visits: 53
|
|
To expand on ianhenderson's way here is the loop without worrying about the lowest id and just grabbing the next one that doesn't have a count.
DECLARE @TableName VARCHAR(255);
SELECT TABLE_NAME AS TableName, null AS RowCnt INTO #TempTableCounts FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'base table';
WHILE (EXISTS(SELECT * FROM #TempTableCounts WHERE (RowCnt IS NULL))) BEGIN SELECT TOP(1) @TableName = TableName FROM #tempTableCounts WHERE (RowCnt IS NULL); EXEC ('UPDATE #TempTableCounts SET RowCnt = (SELECT count(*) FROM ' + @TableName + ') WHERE (TableName = ''' + @TableName + ''')'); END
SELECT TableName, RowCnt FROM #TempTableCounts;
DROP TABLE #TempTableCounts; -David
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, June 14, 2013 6:50 AM
Points: 14,
Visits: 540
|
|
Does this seemingly simpler, and far faster, method produce accurate results?
SELECT name [Table], sum(row_count) AS [Rows] FROM sys.dm_db_partition_stats WITH (NOLOCK), sysobjects WITH (NOLOCK) WHERE index_id < 2 -- Just grab index 0 (heap) or 1 (cluster) AND xtype = 'U' AND object_id = object_id('' + name + '') GROUP BY name
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, May 03, 2013 9:39 AM
Points: 9,
Visits: 20
|
|
| Nice to see I started a healthy debate! I'm going to bear this stuff in mind - I'm always looking for smarter-running code!
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Tuesday, May 07, 2013 1:03 PM
Points: 485,
Visits: 53
|
|
ron.mcdowell that is an awesome script. My initial testing shows it is significantly faster than my script and for all my tests it was accurate. Now I need to learn how/why it works 
-David
|
|
|
|