how to swap the values of two rows in a table

Hi All,

Iam a newbie to Sql server and i am having a task where my current table with ID column as primary key looks like below:

ID  PlateNo Type Image Name
27 455 User img1.jpg
32 542 Alternative img2.jpg

And i want a sql query to modify my table so that the data should be like as shown below:

ID  PlateNo Type Image Name
27 542 Alternative img2.jpg
32 455 User img1.jpg

Kindly help me on the same.

Thanks,

Ram

September 10th, 2015 2:28am

create table #t(id int,c char(1))

insert into #t values (1,'a'),(2,'b')

update #t set id= case when id=1 then 2
                       when id=2 then 1 end 
Free Windows Admin Tool Kit Click here and download it now
September 10th, 2015 2:37am

Hi,

First of all thanks for the reply. The solution you gave does not work for me. Actually my requirement is that except ID column in my table all the other multiple values should be swapped. could you please provide a query that works for my question. Actually iam a newbie to Sql server and i apreciate your detailed help on the same.

Thanks,

Ram

September 10th, 2015 2:54am

There is an ambiguity in your question. If you have many rows in your table, how do you identify which rows need to be taken for swapping?

You may explain your requirement to help you better.

Free Windows Admin Tool Kit Click here and download it now
September 10th, 2015 2:57am

Hi Latheesh,

I always have only two rows in my table, so that is not a problem at all. Could you please let me know the query that works for the same.

Thanks

September 10th, 2015 2:59am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics