SQL Server ROW_NUMBER() on SQL Server 2000?

后端 未结 4 1871
终归单人心
终归单人心 2020-12-03 15:38

I have a query that allows me to get records from a database table by giving it a minimum and maximum limit.

It goes like this:

  SELECT T1.CDUSUARIO         


        
4条回答
  •  天命终不由人
    2020-12-03 15:47

    This is my solution to the problem:

    declare @i int
    declare @t table (row int, stuff varchar(99))
    insert into @t
    select 0,stuff from mytable -- <= your query
    set @i=0
    update @t set row=@i, @i=@i+1
    select * from @t
    

    Explanation:

    1. create a memory table
    2. insert data (your query) with the row number as 0
    3. update the row number field with an int variable which is incremented in the same update for the next record (actually the variable is incremented first and then updated, so it will start from 1)
    4. "select" the result from the memory table.

    You may ask, why don't i use the variable in the select statement? It would be simpler but it's not allowed, only if there is no result. It's ok to do it in an update.

提交回复
热议问题