I have two tables which has many columns and those columns have nulls to in many cases.
What is the best way to compare two tables data?
I am using below way:
UPADTE TableB
SET Col1 = A.Col1
FROM TableA A INNER JOIN TableB B
ON A.Col2 = B.Col2 AND A.Col3 = B.Col3 .... A.Col47 = B.Col47
Somehow I can write this too but as you know SQL Server cannot say NULL = NULL and when I am keeping ISNULL function on all columns, the query is taking 20-30 min.
Is there any way to do the same task much faster and precisely?
This is not totally true. You can potentially set ansi_nulls off for the purposes of this comparison. Note that this feature may not be available someday in a future version of SQL Server, but it’s in there up through 2012 at least.
If this does not solve your performance issue, you may want to look into indexing or better hardware to get the query to run faster, or stepping back to reanalyze what is going on and then refactoring the solution. At first glance, this seems like an unusual operation to perform (copying data between very similar tables, but only on a match of all of that data), but providing the exact table defs, the requirements, and an example might go a long way toward helping others see what could be changed.