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 replication with database mirroring? : sql server database administration
what is denormalization and when would you go for it? : Sql server database administration
What is blocking?
What are the properties and different types of sub-queries?
In how many ways you can invoke ssrs reports?
What is the Difference Between Primary and Foreign Key?
Explain what are page splits? : SQL Server Architecture
What is row_number function?
Please differentiate between a local and a global temporary table?
What is the importance of concurrency control?
What is an expression in ms sql server?
What are the different types of cursor?
what is the difference between them (ethernet networks and token ring networks)? : Sql server database administration
How to write character string constants or literals in ms sql server?
What is the exact numeric data type in sql?