2.
Add a column titled 'New Salesrep ID' so they can fill out new information if it is not a one-to-one swap.
3.
Once you have the new information update @old_salesrep, @new_salesrep and @customer_ls udpated information and run the query. Test in Play first:
DECLARE @old_salesrep VARCHAR(16)
DECLARE @new_salesrep VARCHAR(16)
DECLARE @customer_ls VARCHAR(MAX)
DECLARE @delimiter VARCHAR(10)
SET @old_salesrep = '1071'
SET @new_salesrep = '20924'
SET @customer_ls = '142144,142760,141633,141683,143758,143700,141993,142673,141730,148286,144019,148291,141710,143307,148833,143458,148816,144398,141475,141477,142593,141865,143132,143200,143524,148995,144178'
SET @delimiter = ','
----------------- SHIPTO -----------------
----- If the new salesrep dont exist just change the existing.
UPDATE ship_to_salesrep
SET salesrep_id = @new_salesrep,
date_last_modified = CURRENT_TIMESTAMP,
last_maintained_by = 'raguirre'
FROM ship_to_salesrep
INNER JOIN ship_to ON ship_to_salesrep.ship_to_id = ship_to.ship_to_id AND ship_to.delete_flag = 'N'
INNER JOIN customer ON ship_to.customer_id = customer.customer_id
INNER JOIN p21_fn_split(@customer_ls, @delimiter) AS customer_ls ON customer_ls.value = customer.customer_id_string
WHERE ship_to_salesrep.salesrep_id = @old_salesrep
AND NOT EXISTS
(
SELECT 1
FROM ship_to_salesrep t
WHERE t.ship_to_id = ship_to_salesrep.ship_to_id
AND t.salesrep_id = @new_salesrep
AND t.company_id = ship_to_salesrep.company_id
)
--- Set other salesreps as secondary
UPDATE ship_to_salesrep
SET primary_salesrep = 'N',
date_last_modified = CURRENT_TIMESTAMP,
last_maintained_by = 'raguirre'
FROM ship_to_salesrep
INNER JOIN ship_to ON ship_to_salesrep.ship_to_id = ship_to.ship_to_id AND ship_to.delete_flag = 'N'
INNER JOIN customer ON ship_to.customer_id = customer.customer_id
INNER JOIN p21_fn_split(@customer_ls, @delimiter) AS customer_ls ON customer_ls.value = customer.customer_id_string
WHERE ship_to_salesrep.salesrep_id <> @new_salesrep
--- Set main salesreps as primary
UPDATE ship_to_salesrep
SET primary_salesrep = 'Y',
delete_flag = 'N',
date_last_modified = CURRENT_TIMESTAMP,
last_maintained_by = 'raguirre'
FROM ship_to_salesrep
INNER JOIN ship_to ON ship_to_salesrep.ship_to_id = ship_to.ship_to_id AND ship_to.delete_flag = 'N'
INNER JOIN customer ON ship_to.customer_id = customer.customer_id
INNER JOIN p21_fn_split(@customer_ls, @delimiter) AS customer_ls ON customer_ls.value = customer.customer_id_string
WHERE ship_to_salesrep.salesrep_id = @new_salesrep
----------------- CUSTOMER -----------------
---- If the new salesrep dont exist just change the existing.
UPDATE customer_salesrep
SET salesrep_id = @new_salesrep,
date_last_modified = CURRENT_TIMESTAMP,
last_maintained_by = 'raguirre'
FROM customer_salesrep
INNER JOIN customer ON customer_salesrep.customer_id = customer.customer_id
INNER JOIN p21_fn_split(@customer_ls, @delimiter) AS customer_ls ON customer_ls.value = customer.customer_id_string
WHERE customer_salesrep.salesrep_id = @old_salesrep
AND NOT EXISTS
(
SELECT 1
FROM customer_salesrep t
WHERE t.customer_id = customer_salesrep.customer_id
AND t.salesrep_id = @new_salesrep
AND t.company_id = customer_salesrep.company_id
)
--- Set other salesreps as secondary
UPDATE customer_salesrep
SET primary_salesrep_flag = 'N',
date_last_modified = CURRENT_TIMESTAMP,
last_maintained_by = 'raguirre'
FROM customer_salesrep
INNER JOIN customer ON customer_salesrep.customer_id = customer.customer_id
INNER JOIN p21_fn_split(@customer_ls, @delimiter) AS customer_ls ON customer_ls.value = customer.customer_id_string
WHERE customer_salesrep.salesrep_id <> @new_salesrep
--- Set main salesreps as primary
UPDATE customer_salesrep
SET primary_salesrep_flag = 'Y',
row_status_flag = 704,
date_last_modified = CURRENT_TIMESTAMP,
last_maintained_by = 'raguirre'
FROM customer_salesrep
INNER JOIN customer ON customer_salesrep.customer_id = customer.customer_id
INNER JOIN p21_fn_split(@customer_ls, @delimiter) AS customer_ls ON customer_ls.value = customer.customer_id_string
WHERE customer_salesrep.salesrep_id = @new_salesrep
UPDATE customer
SET salesrep_id = @new_salesrep,
date_last_modified = CURRENT_TIMESTAMP,
last_maintained_by = 'raguirre'
FROM customer
INNER JOIN p21_fn_split(@customer_ls, @delimiter) AS customer_ls ON customer_ls.value = customer.customer_id_string
WHERE salesrep_id = @old_salesrep
NOTE: the delimiter has to be a space with no commas. If you have a large volume of changes to make, you will have to use Find and Replace to update them. You might have to transpose the data in Excel first then use Find and Replace in Notepad++.
To check the data take the original query you used to send the data to the requestor and confirm the current salesrep matches what the requestor filled out in the 'New Salesrep' Column