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

What are the different index configurations a table can have?

0 Answers  


What is the default value of CHAR type?

10 Answers   Bosch,


Give an example of SQL injection attack ?

0 Answers   HAL,


What to perform pattern match with the like operator?

0 Answers  


What is sub-query in sql server? Explain its properties.

0 Answers  


What happens if null values are involved in arithmetic operations?

0 Answers  


What do you understand by replication in sql server? Mention the different types of replication in sql server.

0 Answers  


What's new in sql management studio for sql server? : sql server management studio

0 Answers  


What is the syntax for encrypting a column in SQL Server?

0 Answers   MindCracker,


How to connect sql server management studio express to sql server 2005 express?

0 Answers  


How to reaname table name without using sp_Rename in sql server..?

0 Answers   Wipro,


How do I start sql server agent automatically?

0 Answers  


Categories