Consolidating values in a junction table

Posted by senloe on Stack Overflow See other posts from Stack Overflow or by senloe
Published on 2010-04-20T20:24:09Z Indexed on 2010/04/21 18:03 UTC
Read the original article Hit count: 204

I have the following schema:

Parcels       Segments        SegmentsParcels
=========     ==========      =================
ParcelID      SegmentID       ParcelID
...           Name            SegmentID
              ...             id

A user of the data wants to consolidate Segments.Names and gave me a list of current Segment.Names mapped to new Segment.Names (all of which currently exist).

So now I have this list in a temporary table with the currentID and newID to map to.

What I want to do is update the SegmentID in SegmentsParcels based on this map. I could use the statement:

update SegmentParcels set segmentID = [newID] from newsegments where segmentID = currentid

but this will create some duplicates I have a unique constraint on ParcelID and SegmentID in SegmentParcels.

What is the best way to go about this? I considered removing the constraint and then dealing with removing the duplicates (which I did at one point and could probably do again) but I was hoping there was a simpler way.

© Stack Overflow or respective owner

Related posts about tsql

Related posts about unique-constraint