I'm trying to find a syntax that will take data from one table, compare it to another table, and return any anomalies that exist. Is this possible?

    Requires Free Membership to View

Most definitely possible.

select A.pk
     , A.col1, B.col1
     , A.col2, B.col2
     , A.col3, B.col3
  from table_one as A
inner
  join table_two as B
    on B.pk = A.pk
 where A.col1 <> B.col1 
    or A.col2 <> B.col2 
    or A.col3 <> B.col3 
union all
select A.pk
     , A.col1, null
     , A.col2, null
     , A.col3, null
  from table_one as A
left outer
  join table_two as B
    on B.pk = A.pk
 where B.pk is null
union all
select B.pk
     , null, B.col1
     , null, B.col2
     , null, B.col3
  from table_two as B
left outer
  join table_one as A
    on A.pk = B.pk
 where A.pk is null

The inner join, with its WHERE conditions, returns rows which exist in both tables but which have at least one column different. Then the two outer joins, with their WHERE conditions, return rows which exist in one table but not the other. The results of these three subqueries are combined with UNION ALL to return all anomalies.

This was first published in March 2007

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: