An inner join will only return data that already exists in both places, but an outer - ie left or right - join will return data that exists in either table, and let you write simple formulas to determine whether any piece of data is in one table, the other, or both. A union is similar, but in a different format, and probably one that wouldn't be as useful if you want to compare things; it's easier to do that across columns than across rows.
I'm not really sure why you would want to use a database cursor? I can see how that would be an approach, but it's procedural, or step by step, instead of set based. So it's very much not what SQL was optimized for.
Without having used bulk transfers within SQL to read files in a long time, this is pseudo code, but you could accomplish what you're talking about with something like this:
Code:
-- Load CSV file into a temp table
bcp "c:\filename.csv" into #rawData
-- Update existing but different rows
Update Cars Set Field1 = csv.Field1, ... From #rawData As csv Join Cars On csv.vin = Cars.vin Where csv.Field1 != Cars.Field1 Or csv.Field2 != Cars.Field2 ...
-- Add data for automobiles that don't already have records by VIN
Insert Into Cars Select csv.* From #rawData As csv Left Join Cars On csv.vin = Cars.vin Where Cars.vin Is Null
That will save you from having to deal with a cursor, and probably be easier to maintain.
|