Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2004
    Location
    UK
    Posts
    40

    Unanswered: Incrementing a field in SQL

    Hello,
    I am making adjustments to a table in my SQL Server Database.
    I'm adding a collumn to my database of numeric type.
    I wish to write a SQL script that will go through every record in the database and give it a unique number (key) by simply incrementing each number by 1.
    I've inherited this database and as you have probably guessed i'm trying to add a primary key to this table as currently there isn't one!

    I have no experience what so ever with T-SQL (which I presume i will need to use) as my expertise lie in web development with fairly simple SQL so go easy on me

    Thanks in advance for your help

  2. #2
    Join Date
    Apr 2004
    Posts
    73
    You could set the DataType field to "int" and check the Identity box and set seed to "1" this will increment field by 1.

  3. #3
    Join Date
    Jan 2004
    Posts
    67
    I suggest,

    How can it be done ?
    ---------------------
    Using a Stored Procedure

    What should the Stored Procedure contain?
    ------------------------------------------
    logic that can read every record from the table and add 1 to that and
    make it Unique.

    I have a question here.
    What happens if the Numeric column that u mentioned has the same value across 2 rows ?

  4. #4
    Join Date
    Jan 2003
    Location
    India
    Posts
    523
    why do u need a stored procedure for it?
    just as thatdude mentioned make it an identity column and sql server will assign the values to each record.

  5. #5
    Join Date
    Apr 2004
    Location
    UK
    Posts
    40
    Quote Originally Posted by shankar
    I have a question here.
    What happens if the Numeric column that u mentioned has the same value across 2 rows ?
    The application would start deleting/updating more rows than it should - atleast it will do once i've written the upgrade.

    Thanks to you all for your help - The identity column was a success

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •