I have to exchange the column data from one set of rows to another set of rows
Can anyone explain?
I have to exchange the column data from one set of rows to another set of rows
Can anyone explain?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jayraj ChhayaRank #304 · 8.2K reputationPosted Dec 4, 2024, 1:22 AM EST
Hi Kiran Kumar,
To exchange column data between two sets of rows in SQL, you can utilize a temporary variable or a common table expression (CTE) to facilitate the swap. Below is a simple example using a temporary variable approach.
Assuming you have a table named
Employeeswith columnsID,Name, andSalary, and you want to exchange theSalaryvalues of two specific employees (e.g., IDs 1 and 2):-- Step 1: Store the Salary of the first employee in a temporary variable DECLARE @TempSalary DECIMAL(10, 2); SELECT @TempSalary = Salary FROM Employees WHERE ID = 1; -- Step 2: Update the Salary of the first employee with the second employee's Salary UPDATE Employees SET Salary = (SELECT Salary FROM Employees WHERE ID = 2) WHERE ID = 1; -- Step 3: Update the Salary of the second employee with the temporary variable UPDATE Employees SET Salary = @TempSalary WHERE ID = 2;This method ensures that the
Salaryvalues are exchanged without losing any data. Always ensure to back up your data before performing such operations to prevent any accidental data loss.Jaish MathewsRank #225 · 11K reputationPosted Dec 4, 2024, 4:59 AM EST
You ,may try using CASE as below, I didn't execute this though.
UPDATE Employees
Notes:SET Salary = CASE
WHEN ID = 1 THEN (SELECT Salary FROM Employees WHERE ID = 2)
WHEN ID = 2 THEN (SELECT Salary FROM Employees WHERE ID = 1)
END
WHERE ID IN (1, 2);
ID).SELECTstatements inside theCASEclause work correctly because the SQL engine evaluates them independently for each row before performing theUPDATE.Kiran KumarRank #1,268 · 1.4K reputationPosted Dec 4, 2024, 4:35 AM EST
Hi Jay
I accept your solution but is there any way we can get this done by not using temp table
Also, just for an update on the question I can see your code it update only specific column row value that is by id, but I need to exchange a set of rows to be updated example by departments within the same table
Example Data stored in the format of specific employee's designation not entered for specific department I want that to bring from another department's role and that department roles have to be updated by the charging first time department rows
It is kind of exchanges the same set of data bw rows for that particular group of rows
Kiran KumarRank #1,268 · 1.4K reputationPosted Dec 4, 2024, 8:01 AM EST
Hi Mathews
Thanks for simplified it into single query but again it will update only one record by Id right, not the group of rows
Here is my table
All IT cells should be changed to Admin
And
All Admin cells should be changed to IT
Condition
There should not be any change in the Id column as its primary unique
This scenario where users have selected or entered different department by mistake and that need to be corrected
Please provide the query