Search code examples
sqlsql-server-2000

How to correct added records to the table with correct order number on SQL Server 2000?


How to correct added records to the table with correct order number on SQL Server 2000?

I have the following ordered table:

OrderNo Data
      0    A
      1    B
      2    C

I need to add the following records to the table (with order saving):

OrderNo Data
      2    E
      3    F
      1    D

And to get the following as the result on the table:

OrderNo Data
      0    A
      1    B
      2    C
      3    D
      4    E
      5    F

How to do this on SQL Server 2000?

P.S. OrderNo can't be identity or unique.


Solution

  • Simulating ROW_NUMBER in SQL Server 2000

    select (select count(*) 
            from Table1 as e2
            where e2.OrderNo <= e1.OrderNo) as OrderNo, e1.Data
    from Table1 as e1
    ORDER BY OrderNo