How can you swap values between two rows in a table using single- SQL statement?

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What is a synonym for manipulation?

544


What is the impact on other user sessions when creating indexes?

536


Stored Procedure returns data from multiple tables. How to access it in your ASP.Net code?

546


What is user-defined scalar function?

553


Some queries related to SQL

584






What are the differences between char and varchar in ms sql server?

572


How to get a list of all tables with "sys.tables" view in ms sql server?

547


You accidentally delete the msdb database what effect does this have on your existing sql databases, and how do you recover?

543


Write query to return all rows sql?

613


What is an identity column in insert statements?

581


Can a stored procedure call itself or recursive stored procedure? How much level sp nesting is possible?

613


Can we call stored procedure in view in sql server?

492


what are the types of indexes? : Sql server database administration

595


Do you know what are the differences between lost updates and uncommitted dependencies?

539


What is the most common trace flags used with sql server?

512