First, throw away your cursor-based solution and start over
Sounds like you have duplicates, certainly in the short numbers, possibly in the full (area + numbers) in the database. It's probably just as easy to eliminate duplicates in both cases, but first-- Update any numbers first to include the area codeUPDATE data SET number = @area + number WHERE postalcode LIKE 'WA9%' AND LEFT(number,1) <> '0' AND LEN(number) < 8
-- Delete any duplicates-- This will require a temporary table unless you have a column with a unique value (CreateDate, ID or whatever). Assuming you have a CreateDate column...DELETE d FROM data d LEFT OUTER JOIN ( SELECT postalcode, number, MIN(CreateDate) as MinCreateDate FROM data GROUP BY postalcode, number ) e ON e.postalcode = d.postalcode AND e.number = d.number AND e.MinCreateDate = d.CreateDate WHERE e.number IS NULL -- Any row without a match is a duplicate
I haven't tried this. Run with test data.