Skip to content
Advertisement

ms sql server how to check table has “id” column and count rows if “id” exist

There are too many tables in my SQL Server db. Most of them have an ‘id’ column, but some do not. I want to know which table(s) doesn’t have the ‘id’ column and to count the rows where id=null if an ‘id’ column exists. The query results may look like this:

How do I write this query?

Advertisement

Answer

Building query:

Copy the output and execute in separate window. UNION ALL could be added to get single resultset.

db<>fiddle demo

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement