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 the difference between substr and charindex in the sql server?

0 Answers  


After creating the cube, if we added a new column to the oltp table then how you add this new attribute to the cube? : sql server analysis services, ssas

0 Answers  


What is the difference between delete and truncate statements?

0 Answers  


You want to implement the one-to-many relationship while designing tables. How would you do it?

0 Answers  


what are the critical issues you have resloved in your company

0 Answers   Wipro,






WHICH INDEX IS FAST CLUSTER AND NON CLUSTER INDEX

4 Answers   CarrizalSoft Technologies, L&T,


write a query for list of owner who are having multiple bikes in below table 1 shanker pulsar 2 shanker Honda 3 shanker car 4 Balu pulsar 5 Balu Honda 6 Balu car 7 Shyam pulsar 8 Jaya Honda 9 Deepa car 10 vasu car

12 Answers   Cognizant, TCS,


How to see the event list of an existing trigger using sys.trigger_events?

0 Answers  


What is the use of commit?

0 Answers  


What are subqueries in sql server?

0 Answers  


Can we add a cpu to sql server?

0 Answers  


Tell me about pre-defined functions of sql?

0 Answers   EXL,


Categories