We help IT Professionals succeed at work.

SQL query to count records in a set of tables

156 Views
Last Modified: 2018-09-30
I'm trying to get a count of all SQL tables that start with "comp", but I also want to filter out the count to only records inside these tables that have "Type" column > 0 (some are null, I don't want to count them), and "List" column should be NULL. The query below will get me as far as the count by table name, but I can't figure out how to update the query so it filters the count based on the values as well.

SELECT t.name, s.row_count from sys.tables t
JOIN sys.dm_db_partition_stats s
ON t.object_id = s.object_id
AND t.type_desc = 'USER_TABLE'
AND t.name like 'comp%'
AND s.index_id IN (0,1)
Comment
Watch Question

CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Add a WHERE clause.  I moved the other criteria down to the WHERE clause since it doesn't belong in the join.

SELECT t.name, s.row_count from sys.tables t
JOIN sys.dm_db_partition_stats s
ON t.object_id = s.object_id
WHERE t.Type Is Not Null
AND t.List Is Null
AND t.type_desc = 'USER_TABLE'
AND t.name like 'comp%'
AND s.index_id IN (0,1)

PS - you didn't mention what table Type and List come from so I assumed "t".
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
If you only need the count, there's no good reason to return any data other than count.  It is much more efficient
Select Count(*)

Open in new window

Author

Commented:
The return is
  Invalid column name Type
  Invalid column name List

The first query I posted works, so 't' would suffice (you'd think!), but as stated originally, I'm still having trouble adding any "where" condition to it.

Author

Commented:
select count(*) works for individual tables, but very clunky if you have dozens of tables, especially if you want to run periodic queries.

I also tried
  select count(*) from tablename1
  union select count(*) from tablename2
  etc...
but after the results are skewed -- count is way off.
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
I'm still having trouble adding any "where" condition to it.
It is difficult to guess what might be the problem if you don't post the query and any error or a description of what is wrong with the result.
EugeneZSQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
try this
use yourDB

EXEC [sp_MSforeachtable] @command1='
IF (''?'' LIKE ''comp%]'')
BEGIN
 PRINT ''?''
 SELECT ''?'' TABLENAME,COUNT(*) CNT from ?
 where Type>0 and List is NULL END '

Author

Commented:
Eugene Z, I tried your query and it's still a no go - the errors I get is as follows

Invalid column name 'Type'
Msg 207, Level 16, State 1, Line 6
Invalid column name 'List'
Msg 207, Level 16, State 1, Line 6
EugeneZSQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
are "type" "list" names  of columns in your tables?

you posted;
"also want to filter out the count to only records inside these tables that have "Type" column > 0 (some are null, I don't want to count them), and "List" column should be NULL.

Author

Commented:
yes, they're column names
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
I think Type is a reserved word and List might also be.  It is always best to never use reserved words as column names even if the db engine allows it.

Try encasing the reserved words in single quotes or square brackets.

Author

Commented:
I renamed the column names to pType and pList, I also updated the query with these new names, and still get the same error.
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
It would be helpful if you would post the complete query each time you modify it.

Author

Commented:
EXEC [sp_MSforeachtable] @command1='
IF (''?'' LIKE ''comp%]'')
BEGIN
 PRINT ''?''
 SELECT ''?'' TABLENAME,COUNT(*) CNT from ?
 where pType>0 and pList is NULL END '

also tried the previous suggestion


SELECT t.name, s.row_count from sys.tables t
JOIN sys.dm_db_partition_stats s
ON t.object_id = s.object_id
WHERE t.pType Is Not Null
AND t.pList Is Null
AND t.type_desc = 'USER_TABLE'
AND t.name like 'comp%'
AND s.index_id IN (0,1)
EugeneZSQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
It is possible that some of your “Comp” tables do not have these columns.

Check your tables and search table criteria

Author

Commented:
I reduced the query so only six tables would get queried (I added more characters to the tablename), and confirmed the tables all have these columns. Same error. Note - of the two columns - one of them has datatype INT and the other BIT -- would this make a difference?
EugeneZSQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
Try to run this against of table:
Select count(*) from your table where ..
EugeneZSQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
And post exact error text

Author

Commented:
No error, I get an actual count that is accurate.

As I posted 3d ago, I also tried
  select count(*) from tablename1
  union select count(*) from tablename2
  etc...

after the first table or two, the results are skewed -- count is WAY off.
EugeneZSQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
Add “where yourcolum is null...” to your union t-sql
EugeneZSQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
And try the opposite with posted queries ( without “where”)
EugeneZSQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
If you got with “where” errors “ there are not such fields “
It means these tables do not have such fields

Author

Commented:
As I posted 3d ago, I also tried
  select count(*) from tablename1
  union select count(*) from tablename2
  etc...
this query does not get me the proper totals, even if I use a 'where' condition (columns are found), which is why I posted

I tried the suggested queries, but the column names are not found.

I use SSMS, SQL back end. What do you mean t-sql?

Author

Commented:
I verified the columns specified in query exist in tables, so that's not the problem.
EugeneZSQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
Make sure that you ran in the right db

Author

Commented:
Yep - confirmed.
EugeneZSQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
are you getting the expected result if you run?


use yourDB
EXEC [sp_MSforeachtable] @command1='
IF (''?'' LIKE ''comp%]'')
BEGIN
 PRINT ''?''
 SELECT ''?'' TABLENAME,COUNT(*) CNT from ?
 END '

Author

Commented:
Only says "Query completed successfully." in the status bar at bottom of SSMS. Nothing shows up as a table.
EugeneZSQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
It means it did not find “if-ed criteria” tables in this db an you remove ]from”if”?
EugeneZSQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
use yourDB
EXEC [sp_MSforeachtable] @command1='
IF (''?'' LIKE ''comp%'')
BEGIN
 PRINT ''?''
 SELECT ''?'' TABLENAME,COUNT(*) CNT from ?
 END '
EugeneZSQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
Try this one

Author

Commented:
same thing -
sql1.png
EugeneZSQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
next try:


use yourDB
EXEC [sp_MSforeachtable] @command1='
IF (''?'' LIKE ''%comp%'')
BEGIN
 PRINT ''?''
 SELECT ''?'' TABLENAME,COUNT(*) CNT from ?
 END '
EugeneZSQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
try this -- it should work for you (just adjust  "WHERE" )

EXEC [sp_MSforeachtable]
@command1=
      '
      BEGIN
       PRINT ''?''
       SELECT ''?'' TABLENAME,COUNT(*) CNT from ? where type >2 and list >3
       END '
 ,@whereand =
       ' AND OBJECT_ID in ( select  t.object_id
       from sys.tables t inner join sys.columns c
       ON t.object_id=c.object_id
       where t.name like ''Comp%''
       and C.name in (''List'',''type''))'ee092929018PNG.PNG

Author

Commented:
Thanks, Eugene. This query's more complicated than I'm used to but it seems to be working. I'm getting counts for each table, but they are broken up in separate tables. My intention was to get the totals to show up in one table (kind of like a 'union select' would do) so I can later use them in a report. can this query be modified to join all totals together in one table?
SQL SERVER EXPERT
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
It works!!!  :)
Many thanks!!!!!

Author

Commented:
Thanks, Eugene!!!!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions