I have a table with columns id, items
:
ID Items
----------------
1 mc3
2 mc2
3 mc1
I want to insert this data into a temp table but the order of items in descending ie
ID Items
--------------
1 mc1
2 mc2
3 mc3
but I can't take order by of these items. items
column has duplicate values. I tried to use identity column but that was a terrible idea. I will show that query.
DECLARE @totalNO INT = 0
SET @totalNO = (SELECT COUNT(*) FROM @tblmc)
SELECT IDENTITY(INT, @totalNO, -1) RowIndex1, *
INTO #tempmcfordesc
FROM @tblmc.
but this query causes an error. Is there any other way? I can loop the table and insert but I just want to know is there any easy way for this. Please sort this out.
if you have create the temp table then use this :
INSERT INTO #temptable
SELECT Row_Number()
OVER (
ORDER BY ID DESC) AS ID,
Items
FROM yourtable
and if you haven't created the temp table then create it with data like this :
SELECT Row_Number()
OVER (
ORDER BY ID DESC) AS ID,
Items
INTO #temptable
FROM yourtable
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.