2011-04-13 3 views
0

열 이름 코스가있는 테이블이 있습니다. 2 단차 코스는 "C++"이며 4 단 코스는 "ASP.net"입니다.sql 테이블에서 2 행의 교환 값

업데이트 쿼리를 사용하여 값과 값을 교환하고 싶습니다. 이것을 어떻게 할 수 있습니까?

update YourTable set Course = 'ASP.NET' where id = 2 
update YourTable set Course = 'C++' where id = 4 

나 :

답변

2

당신은 같은 update으로 값을 변경할 수 있습니다 전환

update YourTable 
set  Course = 
     case id 
     when 2 then 'ASP.NET' 
     when 4 then 'C++' 
     end 
where id in (2,4) 
0

테스트 테이블 및 데이터

create table YourTable(id int primary key, course varchar(10)) 

insert into YourTable values (1, 'Delphi') 
insert into YourTable values (2, 'C++') 
insert into YourTable values (3, 'Clipper') 
insert into YourTable values (4, 'ASP.net') 

업데이트 2, 4

update YourTable set 
    course = case id 
      when 4 then (select course from YourTable where id = 2) 
      when 2 then (select course from YourTable where id = 4) 
      else T.course 
      end 
from 
    YourTable as T 
where T.id in (2, 4) 

결과

id course 
1 Delphi 
2 ASP.net 
3 Clipper 
4 C++