I have a table called Employee
Eno ename AttributeValue AttributeName 1 aa a123 abc 2 bbb b123 dcf 3 cc c7sd wew3
I want to swap the data from column AttributeValue
to AttributeName
and AttributeName
to AttributeValue
For Example:
Eno ename AttributeValue AttributeName 1 aa abc a123 2 bbb dcf b123 3 cc wew3 c7sd
SET Col1 = Col2, Col2 = Col1; When you run above update statement, the values of the columns will be swapped in SQL Server. There is no need for temporary column, variable or storage location in SQL Server. You can validate that with the SELECT statement here.
In such a case, you can use the following UPDATE statement syntax to update column from one table, based on value of another table. UPDATE first_table, second_table SET first_table. column1 = second_table. column2 WHERE first_table.id = second_table.
No need for multiple statements or variables, this can be done in a single statement: update emp set phone_number = case when 205 then (select phone_number from emp where employee_id = 209) when 209 then (select phone_number from emp where employee_id = 205) end where employee_id in (205, 209);
UPDATE employee SET AttributeValue = AttributeName, AttributeName = AttributeValue
However, unless both columns have the exact same definition, you risk losing information.
Update employee Set attributeValue = attributeName, attributeName = attributeValue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With