How to have a line number without cursor

  • Hi,

    I have a general question about sql. In oracle, it exist a rownumber function that we allow see the line number, I would like to have same thing in mssql without cursor. Is it possible?

    Ex

    Select …… from …….

    Go

    N name ……

    1 qdff

    2 aze

    … ….

    N Zxxxx

    Thank in advance

  • No, there is no built in function. An alternative to a cursor is to insert the result-set into a temp table which has an identity field (1,1). Then select from the temp table and your result-set will be numbered as you want.

    Regards,

    Andy Jones

    .

  • Here is another way to set a row number:

    use northwind

    go

    select count(*) row_num, a.customerid,a.employeeid from

    (select customerid, employeeid from Northwind.dbo.orders group by customerid,employeeid) a,

    (select customerid, employeeid from Northwind.dbo.orders group by customerid,employeeid) b

    where a.customerid+cast(a.employeeid as char) > b.customerid+cast(b.employeeid as char)

    group by a.customerid, a.employeeid

    order by 2,3

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply