T-SQL query, comparing data in two tables

I am comparing data in two identical tables, Ideally the data should be the same but there are some differences. I found those rows which are different using EXCEPT (there are multiple ways to do this).
If the row has not matching data then: some of this row columns have the same data and some have not the same data.
I'd like to write sql code that for every not matching row returns a list of not matching columns.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
Could you give an example of the input and output you are looking for?
If the two fields are identical between the two different tables then I'm using 1, otherwise, 0.  This example compares 3 fields (fld1, fld2, fld3):
SELECT	pk	=	ISNULL(a.pk,b.pk)
,	fld1	=	CASE WHEN a.fld1 = b.fld1 THEN 1 else 0 END
,	fld2	=	CASE WHEN a.fld2 = b.fld2 THEN 1 else 0 END
,	fld3	=	CASE WHEN a.fld3 = b.fld3 THEN 1 else 0 END
FROM	tableA	a
JOIN	tableB	b	ON	a.pk = b.pk

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.