The database table having Column1 and Column2. Fetch the values and map the column (Column1 and Column2) details and update in Column3
Column3 should have Column1 value where the Column1 value is equal to Column2 value. Please refer the attached image for better understanding.
Example1: Column1 holds 914702(Row#2) the same value is available in Column2(Row#1). So the Column3 for Row#2 should update the value of Column1 in Row#1 which is 914703
Example2: Column1 holds 914698(Row#5) the same value is available in Column2(Row#3 & 4). So the Column3 in Row#5 should holds the values of Column1 in Row#3 & 4 which is 914700, 914701.
Able to achieve this with below mentioned query and WHILE loop.
The table having 100s of Rows. Using while loop will impact the performance. Will we able to achieve this with UPDATE statement in SQL Server.
DECLARE @Variable VARCHAR(MAX) = ''
SELECT @Variable = @Variable + 'Value: ' + CONVERT(VARCHAR, Column1) + ', '
FROM Table1 WHERE Column2 = @Value
UPDATE Table1
SET Column3 = SUBSTRING(@Variable, 0, LEN(@Variable) - 1)
WHERE Column1 = @Value
Expected Output