Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Running a sub query on each row returned

Author  Topic 

Just_Jeff
Starting Member

7 Posts

Posted - 2011-11-14 : 18:24:59
I want to create a query that will show the number of records in each column that has a NULL value for each table in my database.

I've got the first part figured out.

select t.TABLE_NAME
,c.COLUMN_NAME
from INFORMATION_SCHEMA.TABLES t inner join INFORMATION_SCHEMA.COLUMNS c on (t.TABLE_NAME = c.TABLE_NAME)
where t.TABLE_CATALOG = 'MyDB'
order by t.TABLE_NAME, c.COLUMN_NAME


Now I need to squeeze the following query into the previous SQL statement so that it operates on each row returned:

select count(*) from currentTable where currentColumn is NULL

The end product should be a big list with all the tables, each column in those tables and the row counts of null values in each column.

I'm sure the query is something simple but I don't seem to be able to get it to work for each row.


Thanks,
-Jeff

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-11-14 : 18:38:06
Try this:

select 'select ''' + t.TABLE_NAME + ''', ''' + c.COLUMN_NAME + ''', count(*) from ' + t.TABLE_NAME + ' where ' + c.COLUMN_NAME + ' is NULL'
from INFORMATION_SCHEMA.TABLES t inner join INFORMATION_SCHEMA.COLUMNS c on t.TABLE_NAME = c.TABLE_NAME
where t.TABLE_CATALOG = 'MyDB' and c.IS_NULLABLE = 'YES'
order by t.TABLE_NAME, c.COLUMN_NAME

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Just_Jeff
Starting Member

7 Posts

Posted - 2011-11-14 : 18:56:41
That's sort of what I'm after but I'd like it to execute each one of those statements and return just 3 columns. Table, column, null record count.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-11-14 : 18:57:47
Well all you have to do is copy/paste the output and then run it. What I posted is what generates the code for you. You have to run the output.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Just_Jeff
Starting Member

7 Posts

Posted - 2011-11-14 : 19:13:53
The problem is that it returns a resultset for each sql statement. That gives me 678 recordsets and that isn't going to work very well for this project. Even throwing in a union statement between all of the statements isn't a very elegant solution.

There must be a way to run a subquery within the main query.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-11-14 : 19:17:27
Adding in the union all to my query will be as simple as you can get this. Elegant, well that's subjective. Simple, yes. Achieves what you want, yes.

Any other solutions will involve looping and putting the results into a table and later querying said table.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -