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

can an order by clause be used in a creation of a view?

0 Answers  


Do you know what is xpath?

0 Answers  


Explain features of analysis services?

0 Answers  


How to list all triggers in the database with sys.triggers in ms sql server?

0 Answers  


Explain the collation?

0 Answers  






In the primary key have a multiple field or not?

8 Answers   TCS,


List out the difference between union and union all in sql server?

0 Answers  


Delete duplicate records from the table?(Table must have unique id)

7 Answers   TCS, Thomson Reuters,


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

3 Answers  


What is the xml datatype?

0 Answers  


What can be used instead of trigger?

0 Answers   ADITI,


What is @@error in sql?

0 Answers  


Categories