Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. records with their row numbers in a table

records with their row numbers in a table

Scheduled Pinned Locked Moved Database
databasesql-serveroraclecomsysadmin
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    indian143
    wrote on last edited by
    #1

    Can anybody please tell me in SQL Server how should I get the records with their row numbers in a table. Like we use Rownum in Oracle. Thank you. Regards, Mohd. Abdul Aleem,

    S/W Engineer Akebono Soft Technologies aleem_abdul@akebonosoft.com.

    C 1 Reply Last reply
    0
    • I indian143

      Can anybody please tell me in SQL Server how should I get the records with their row numbers in a table. Like we use Rownum in Oracle. Thank you. Regards, Mohd. Abdul Aleem,

      S/W Engineer Akebono Soft Technologies aleem_abdul@akebonosoft.com.

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      indian143 wrote:

      Can anybody please tell me in SQL Server how should I get the records with their row numbers in a table.

      You need SQL Server 2005. However, you should realise that row numbers are a fiction. Oracle and SQL Server 2005 supply a number based on the order that the rows are returned and have little (only incidental) or no bearing on how the data is actually stored. Anyway, the answer is ROW_NUMBER[^]


      Upcoming events: * Glasgow: Geek Dinner (5th March) * Edinburgh: Web Security Conference Day for Windows Developers (12th April) My: Website | Blog | Photos

      I 1 Reply Last reply
      0
      • C Colin Angus Mackay

        indian143 wrote:

        Can anybody please tell me in SQL Server how should I get the records with their row numbers in a table.

        You need SQL Server 2005. However, you should realise that row numbers are a fiction. Oracle and SQL Server 2005 supply a number based on the order that the rows are returned and have little (only incidental) or no bearing on how the data is actually stored. Anyway, the answer is ROW_NUMBER[^]


        Upcoming events: * Glasgow: Geek Dinner (5th March) * Edinburgh: Web Security Conference Day for Windows Developers (12th April) My: Website | Blog | Photos

        I Offline
        I Offline
        indian143
        wrote on last edited by
        #3

        Could you please tell me that dont I have any other option to get it SQL Server 2000. Please. Thank you. Regarsd,

        S/W Engineer Akebono Soft Technologies aleem_abdul@akebonosoft.com.

        X 1 Reply Last reply
        0
        • I indian143

          Could you please tell me that dont I have any other option to get it SQL Server 2000. Please. Thank you. Regarsd,

          S/W Engineer Akebono Soft Technologies aleem_abdul@akebonosoft.com.

          X Offline
          X Offline
          xfitr2
          wrote on last edited by
          #4

          I do not recommend this but you could create a insert/delete trigger that loops through the recordset and inserts the new value. This is a performance hit. A sample trigger is below. You must have a primary key on the table to do this. I am confused of why you need to reference the rownumber. It has no significance. If you dont mind a few holes here and there after you have deleted a record, use an Identity column. CREATE TRIGGER dbo.trg_Count ON dbo._TEST AFTER INSERT, DELETE AS BEGIN SET NOCOUNT ON; DECLARE @PKEY UNIQUEIDENTIFIER DECLARE @I BIGINT IF ((SELECT trigger_nestlevel()) = 1) BEGIN DECLARE TRG_TEST_CUR CURSOR FOR SELECT [GUID] FROM _TEST OPEN TRG_TEST_CUR FETCH NEXT FROM TRG_TEST_CUR INTO @PKEY SET @I = 1 WHILE @@FETCH_STATUS = 0 BEGIN UPDATE _TEST SET ID = @I WHERE [GUID] = @PKEY SET @I = @I + 1 FETCH NEXT FROM TRG_TEST_CUR INTO @PKEY END CLOSE TRG_TEST_CUR DEALLOCATE TRG_TEST_CUR END END GO

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups