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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Server 2005 - Working with IDENTITY column

SQL Server 2005 - Working with IDENTITY column

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelpquestion
4 Posts 4 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.
  • A Offline
    A Offline
    Anusha 1980
    wrote on last edited by
    #1

    Hi, I am working on SQL Server 2005 tables. Here I need to add a column named ‘ID’ as ‘IDENTITY’ column (With starting and incrementing values as 1, 1). Now my problem is that these tables already have thousands of records. So could you please suggest the best and easy way to perform this job? Many Thanks, Regards. Anusha.

    M N L 3 Replies Last reply
    0
    • A Anusha 1980

      Hi, I am working on SQL Server 2005 tables. Here I need to add a column named ‘ID’ as ‘IDENTITY’ column (With starting and incrementing values as 1, 1). Now my problem is that these tables already have thousands of records. So could you please suggest the best and easy way to perform this job? Many Thanks, Regards. Anusha.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Just add the field, SQL server will insert the ID values for existing records (It does in 2008 and I think it did in 2005). If not add the field, use row_number() to update the value and then add the identity constraint.

      1 Reply Last reply
      0
      • A Anusha 1980

        Hi, I am working on SQL Server 2005 tables. Here I need to add a column named ‘ID’ as ‘IDENTITY’ column (With starting and incrementing values as 1, 1). Now my problem is that these tables already have thousands of records. So could you please suggest the best and easy way to perform this job? Many Thanks, Regards. Anusha.

        N Offline
        N Offline
        Niladri_Biswas
        wrote on last edited by
        #3

        Suppose I have a table declare @tbl1 table(name varchar(20), address varchar(50)) insert into @tbl1 select 'John','302 ABC Street' union all select 'Smith','412 DEF Street' select * from @tbl1 The output is

        name address
        John 302 ABC Street
        Smith 412 DEF Street

        Now I want to insert identity Change the table definition by adding an identity field like this

        declare @tbl1 table(id int identity,name varchar(20), address varchar(50))

        The output is

        id name address
        1 John 302 ABC Street
        2 Smith 412 DEF Street

        Else go ahead with the Row_Number() function(since sql server 2005 onwards)

        select ROW_NUMBER() over(order by name) id, * from @tbl1

        :)

        Niladri Biswas

        1 Reply Last reply
        0
        • A Anusha 1980

          Hi, I am working on SQL Server 2005 tables. Here I need to add a column named ‘ID’ as ‘IDENTITY’ column (With starting and incrementing values as 1, 1). Now my problem is that these tables already have thousands of records. So could you please suggest the best and easy way to perform this job? Many Thanks, Regards. Anusha.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Just add the IDENTITY column, SQL Server will fill the values automatically.

          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