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 is collation sensitivity? Explain different types.

0 Answers  


What are the difference between “where” and “having” clause in sql server?

0 Answers  


What is normalization? Describe its different types.

0 Answers   Wipro,


explain different types of backups avaialabe in sql server? : Sql server database administration

0 Answers  


What are the steps to insert a table?

0 Answers  






Explain the use of keyword with encryption. Create a store procedure with encryption?

0 Answers  


How to execute multiple stored procedures at one time in sql server?

0 Answers  


Define cursor locking

0 Answers  


Describe triggers features and limitations?

0 Answers  


Explain what is the purpose of sql profiler in sql server?

0 Answers  


Why we should not use triggers?

0 Answers  


What are indexes? When do you need to create Indexes?

4 Answers   CarrizalSoft Technologies, HP,


Categories