如何交换SQL Server 2008中的列值?

我有一个名为Employee的表

Eno ename AttributeValue AttributeName 1 aa a123 abc 2 bbb b123 dcf 3 cc c7sd wew3 

我想将列AttributeValue的数据交换到AttributeNameAttributeNameAttributeValue

例如:

 Eno ename AttributeValue AttributeName 1 aa abc a123 2 bbb dcf b123 3 cc wew3 c7sd 
 UPDATE employee SET AttributeValue = AttributeName, AttributeName = AttributeValue 

但是,除非两列具有完全相同的定义,否则可能会丢失信息。

 Update employee Set attributeValue = attributeName, attributeName = attributeValue 

update Employee set AttributeValue = AttributeName, AttributeName = AttributeValue

这是一个很好的例子

 SELECT * from employees; Go DECLARE @temp as varchar(20) update employees set @temp = fname, fname = lname, lname = @temp WHERE deptno = 10; GO SELECT * from employees; 

结果

 Declare @myTable Table (id int, first_name varchar(50), last_name varchar(50)); Select * from Student Insert Into @myTable (id, first_name, last_name) Select id, last_name, first_name from Student MERGE INTO Student std USING @myTable tmp ON std.id = tmp.id WHEN MATCHED THEN UPDATE SET std.first_name = tmp.first_name, std.last_name = tmp.last_name; Select * from Student 

产量

查询结果截图

只需在一次更新中交换两列:

 Update registration Set AttributeName = AttributeValue , AttributeValue = AttributeName where id in (1,2,3)