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 xml datatype?
How is sql used in sql server?
How do I find the size of a sql server database?
What are some examples of schemas?
How many tables can be joined in SQL Server?
What is outer join in sql server joins?
what are the reporting service components in SSRS?
How do you implement session management in SQL Server mode?
Write query to return all rows sql?
What are distributed partitioned views?
How to get the query of a table in sql server?
what is raid and what are different types of raid configurations? : Sql server database administration
What is the difference between for auto and for nested?
Explain about sql server login?
What is difference between index and primary key?