0

I have 15 tables and they all have the columns CreationDate and LastModifiedDate is there a way to query all the tables and look for null values in the tables.

This is what I am thinking I have to do but was wondering if there is a more efficient way or easier way to do the following

Select COUNT(*) FROM Table1, Table2, Table3
WHERE table1.CreationDate IS NULL
      OR table1.LastModifiedDate IS NULL
      OR table2.CreationDate IS NULL
      OR table2.LastModifiedDate IS NULL
      OR table3.CreationDate IS NULL
      OR table3.LastModifiedDate IS NULL

2 Answers 2

3
select count(*)
from (
    select CreationDate, LastModifiedDate from Table1
    union all
    select CreationDate, LastModifiedDate from Table2
    union all
    select CreationDate, LastModifiedDate from Table3
) a 
where CreationDate is null or LastModifiedDate is null
Sign up to request clarification or add additional context in comments.

3 Comments

Very clever - +1 on that one!
Would there be an easy way to update those times that are null to GETDATE()?
A standard update statement will do this. Ask as a new question if you are unsure how.
0

You original query does not do what you expect. It does a cross join among all the rows that have missing values. This can result in a ton of data (if all tables have multiple rows) or none at all (if one has no missing rows).

I assume that you want to know the table name where things are missing:

select tablename, count(*),
       sum(case when CreationDate is null then 1 else 0 end) as MissingCreationDate,
       sum(case when LastModifiedDate is null then 1 else 0 end) as MissingLastModifiedDate
from ((select 'table1' as tablename,  CreationDate, LastModifiedDate
       from Table1 
      ) union all
      (select 'table2' as tablename, CreationDate, LastModifiedDate
       from Table2
      ) union all
      . . .
      (select 'table15', CreationDate, LastModifiedDate
       from Table3
      ) 
     ) t
where CreationDate is null or LastModifiedDate is null
group by tablename

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.