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 Deadlock?

4 Answers   Satyam,


What are the restraints imposed on the table design by a merge replication?

0 Answers  


Explain how to maintain a fill factor in existing indexes?

0 Answers  


What are relationships and mention different types of relationships in the dbms

0 Answers  


How will you go about resolving deadlocks?

0 Answers  


how do we find every fifth record in a table

14 Answers   Aspire, HCL,


Explain important index characteristics?

0 Answers  


What is the use of keyword with encryption.

0 Answers  


How to change the data type of an existing column with "alter table" statements in ms sql server?

0 Answers  


Is resultset an interface?

0 Answers  


How to copy the tables, schema and views from one sql server to another?

0 Answers  


What are the properties and different types of sub-queries?

0 Answers  


Categories