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 'write-ahead log' in sql server 2000 ?
Can we take the full database backup in log shipping?
What is policy management?
What are scheduled tasks in sql server?
What is checkpoint in sql server?
What is transaction server distributed transaction?
What is table level trigger?
Can sql servers linked to other servers like oracle?
What are commonly used mssql functions in php?
What are the types of containers in ssis?
What is dbcc command in sql server?
Can another user execute your local temporary stored procedures?
What does man by sql wildcard characters in sql server?
Explain tablesample?
Is it safe to delete log files?