How can you swap values between two rows in a table using single- SQL statement?



How can you swap values between two rows in a table using single- SQL statement?..

Answer / sush4

CREATE TABLE YourTable
(
ID INT,
PlateNo INT,
[Type] VARCHAR(20),
[Image Name] VARCHAR(20)
);

INSERT INTO YourTable
VALUES
(27,455,'User','img1.jpg'),
(32,542,'Alternative','img2.jpg');
SELECT * FROM YourTable

;WITH Cte AS
(SELECT T.*,T2.PlateNo PlateNo2, T2.Type Type2, T2.[Image Name] [Image Name 2] FROM YourTable T JOIN YourTable T2 ON T.ID<>T2.ID)
UPDATE Cte SET PlateNo = PlateNo2, Type=Type2,[Image Name]=[Image Name 2]


SELECT * FROM YourTable

DROP TABLE YourTable

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More SQL Server Interview Questions

Write the queries for commands like Create Table, Delete table, Drop Table etc.

0 Answers   HPCL, Hughes Systique Corporation,


Explain system functions or built-in functions? What are different types of system functions?

0 Answers  


How to find the date and time of last updated table?

3 Answers  


How do you find value of first column before inserting value into the second column in the same table for checking that second column must have different value than first column.

2 Answers   L&T,


how do u do Performance tunning ?

1 Answers   Infodat Technologies, Satyam,






Can you use order by when defining a view?

0 Answers  


How many types of local tables are there in sql server?

0 Answers  


How does stuff differ from the replace function?

0 Answers  


How can I track the changes or identify the latest insert-update-delete from a table?

0 Answers  


Describe in brief databases and sql server databases architecture.

0 Answers  


what is a join and explain different types of joins? : Sql server database administration

0 Answers  


How to select top 5 coloumn from a table without using coloumn name

5 Answers  


Categories