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
What is a synonym for manipulation?
What is the impact on other user sessions when creating indexes?
Stored Procedure returns data from multiple tables. How to access it in your ASP.Net code?
What is user-defined scalar function?
Some queries related to SQL
What are the differences between char and varchar in ms sql server?
How to get a list of all tables with "sys.tables" view in ms sql server?
You accidentally delete the msdb database what effect does this have on your existing sql databases, and how do you recover?
Write query to return all rows sql?
What is an identity column in insert statements?
Can a stored procedure call itself or recursive stored procedure? How much level sp nesting is possible?
Can we call stored procedure in view in sql server?
what are the types of indexes? : Sql server database administration
Do you know what are the differences between lost updates and uncommitted dependencies?
What is the most common trace flags used with sql server?