You don't even need to do the update, you can do the check and alter the MemberNo
all within the insert to the Target table
INSERT TargetTable (MemberNo, Column1, Column2, Column3)
SELECT MemberNo = CASE WHEN t.MemberNoExists IS NULL THEN i.MemberNo
ELSE n.NextMemberNo + ROW_NUMBER() OVER(PARTITION BY t.MemberNoExists ORDER BY i.MemberNo)
END,
i.Column1,
i.Column2,
i.Column3
FROM ImportTable i
OUTER APPLY
( SELECT TOP 1 MemberNoExists = 1
FROM TargetTable t
WHERE t.MemberNo = i.MemberNo
) t
CROSS JOIN
( SELECT NextMemberNo = MAX(MemberNo)
FROM TargetTable
) n;
The subquery t
simply generates 1 when the MemberNo already exists, and NULL when it doesn't. If it doesn't exist then the MemberNo from the import table is used.
The subquery n
gets the maximum MemberNo
in the target table. This is only used where the MemberNo from the import table already exists. Then, so that you don't need a loop it uses ROW_NUMBER
to increment this maximum MemberNo by one for each new row to avoid duplicates.
Example on SQL Fiddle (Only with SELECT for demonstration purposes)
If you do still need to do the update you can use a similar query:
WITH Import AS
( SELECT NewMemberNo = n.NextMemberNo + ROW_NUMBER() OVER(ORDER BY i.MemberNo),
i.MemberNo
FROM ImportTable i
CROSS JOIN
( SELECT NextMemberNo = MAX(MemberNo)
FROM TargetTable
) n
WHERE EXISTS
( SELECT 1
FROM TargetTable t
WHERE t.MemberNo = i.MemberNo
)
)
UPDATE Import
SET MemberNo = NewMemberNo;