SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,671 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Is there any function that reverse the whole column?
Sample like this:
Result:
Hi @Lylyy,
Please try the following.
-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, [value] INT);
INSERT @tbl ([value]) VALUES
(111), (222), (333);
-- DDL and sample data population, end
-- before
SELECT * FROM @tbl;
-- after
SELECT ROW_NUMBER() OVER (ORDER BY [value] DESC) AS ID
, [value]
FROM @tbl;
pretty simple set operation:
update test
set value = t2.value
from (
select id, ROW_NUMBER() over (order by id) as rn
from test
) t1
join (
select id, value, ROW_NUMBER() over (order by id desc) as rn
from test
) t2 on t1.rn = t2.rn