Easily Find Rows That Violate Constraints

Downloads
94863.zip

One of the known problems in adding constraints to any kind of database is the validation of existing data. For this reason, DBAs often create check, foreign-key, and other types of constraints with the WITH NOCHECK clause. That way, any rows of data that violate the constraint are ignored.

When data validation is important, DBAs must find all the constraint-violating rows so that they can fix them. To find them, they have to use a different SELECT statement for each constraint, which is a tedious task. For example, suppose a table has these check and foreign-key constraints:

ALTER TABLE emp ADD CONSTRAINT
ck_emp_sal CHECK
(salary BETWEEN 4000 and 10000)
 
  ALTER TABLE emp ADD CONSTRAINT  
  FK_emp_mgr FOREIGN KEY
(mgr_id) REFERENCES emp (emp_id)
 

First, the DBA has to run the following SELECT statement to retrieve the rows that violate the check constraint:

SELECT * FROM emp WHERE
salary < 4000 OR salary > 10000

Then, the DBA has to run the following SELECT statement to retrieve the rows that violate the foreign-key constraint:

SELECT * FROM emp WHERE
mgr_id NOT IN
(SELECT emp_id FROM emp)

I wrote a procedure called showViolatingRows to automate the tedious task of finding rows that violate check and foreignkey constraints. This procedure needs only one piece of input: the constraint name that uniquely identifies the table it's declared in.

As the excerpt in Listing 1 shows, the showViolatingRows procedure dynamically activates the DBCC CHECKCONSTRAINTS statement. This statement checks the integrity of a specific constraint or all the constraints for a specified table. The showViolatingRows procedure stores the DBCC CHECKCONSTRAINTS results in a temporary table named ##dbcc.

The DBCC CHECKCONSTRAINTS statement's results consist of three pieces of information: the name of table, the name of the constraint, and column values that identify the rows violating the constraint. You can use these values in a SELECT statement's WHERE clause. Thus, for each constraint-violating row, the showViolatingRows procedure uses a SELECT statement to retrieve that row's data. The procedure stores the results in a global temporary table (##tempResults) that's returned to the user at the end of the showViolatingRows procedure's execution.

On the SQL Server Magazine Web site, you'll find a file named showViolatingRows.sql, which includes the code for the showViolatingRows procedure as well as code that demonstrates how you might use the procedure in a script. I tested showViolatingRows on SQL Server 2005 Standard Edition and SQL Server 2000 Standard Edition from a Windows XP client.
—Eli Leiba

Discuss this Article 3

helen (not verified)
on Jun 13, 2007
the download comes down empty?
befvc4
on Jan 14, 2008
Can not download the code, it gives me an error on www.winnetmag.com not being available...
AnneG_editor
on Jan 29, 2008
The download link appears to be working now. Let us know if you still can't download the code.

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottOur Experts will show you:
• Common SQL Server
Problems
• Best Practices for T-SQL
• SQL Server Integration
Services
• Database Development

Come See Mike Otey & Tim Ford in Person!

Early Registration Now Open

From the Blogs
May 9, 2013
blog

My ISO 8601-Compliant Signature 2

My family recently just "officially" announced that we're in the process of adopting a child from South Africa. We're quite excited, of course, but there's a ton of paperwork to do—along with the need for gobs of signatures....More
May 8, 2013
blog

Use SSIS for ETL from Hadoop

In this blog post, Mark Kromer walks you through using SSIS as a way to use ETL techniques using Microsoft's Hadoop on Windows (HDInsight) as a source using Hive connectors...More
Vision road sign
May 6, 2013
blog

Cheaters Never Win, Even in TPC Benchmarks

In this portion of the series on database benchmarking, I want to tell you about one of my favorite aspects of the TPC benchmarks – CHEATING....More
SQL Server Pro Forums

Get answers to questions, share tips, and engage with the SQL Server community in our Forums.