Alex Rivera | Logout

Is there a quick way to check if ANY column is NULL?

Asked 2012-03-14T15:54:06.667
19

I have a table with around 20 columns. Aside from typing out:

Where column1 is null OR column2 is null OR column3 is null etc...

Is there a quicker way to just check every column and see if any value is null and if so, return that record?

Edit
Report

2 Answers

0

That depends on what quicker means.

If you mean quicker for SQL Server to execute, one thing you could do is write a trigger than updates a bit column that specifies if the entire row (other than the bit and primary key) are NULL. But, there should be a REAL good reason for this as it will impact your update performance. Indexes on those columns would help as well.

If you mean quicker to write, you could have SQL generate the where clause for you. But, unless you do this alot, it isn't worth the time in my opinion.

answered 2012-03-14T16:04:42.183
0

Teaching-to-fish-instead-of-giving-you-the-fish kind of answer here:

One way of doing it is by creating a Stored Procedure that assembles and runs a dynamic query.

The Stored procedure will:

  • have a Table name as input parameter.
  • query the meta data system tables for the specific table structure.
  • dynamically assemble a string (the actual query) with the OR statements for that table's columns.
  • run the assembled query and return the result.
answered 2012-03-14T16:05:57.487

Your Answer