How to list row count of each table in the database. Some equivalent of
select count(*) from table1
select count(*) from table2
...
select count(*) from tableN
I will post a solution but other approaches are welcome
How to list row count of each table in the database. Some equivalent of
select count(*) from table1
select count(*) from table2
...
select count(*) from tableN
I will post a solution but other approaches are welcome
Well luckily SQL Server management studio gives you a hint on how to do this. Do this,
Pause the trace and have a look at what TSQL is generated by microsoft.
In the probably last query you will see a statement starting with
exec sp_executesql N'SELECT
when you copy the executed code to visual studio you will notice that this code generates all the data the engineers at microsoft used to populate the property window.
when you make moderate modifications to that query you will get to something like this:
Now the query is not perfect and you could update it to meet other questions you might have, the point is, you can use the knowledge of microsoft to get to most of the questions you have by executing the data you're interested in and trace the TSQL generated using profiler.
I kind of like to think that MS engineers know how SQL server work and, it will generate TSQL that works on all items you can work with using the version on SSMS you are using so it's quite good on a large variety releases prerviouse, current and future.
And remember, don't just copy, try to understand it as well else you might end up with the wrong solution.
Walter
Output:
You could try this:
The first thing that came to mind was to use sp_msForEachTable
that does not list the table names though, so it can be extended to
The problem here is that if the database has more than 100 tables you will get the following error message:
So I ended up using table variable to store the results
I think that the shortest, fastest and simplest way would be: