How to Increase value of Student ID(int) by 1 automatically?

  • I have table of Student_Details where StudentID is int type and set as Primary Key. I want when any new record innserted in table the value of StudentID will increase by 1 automatically. How can i do it? I try but not getting a solution of it.

  • Make it an "identity" column.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Hello,

    I have already set the identity as Yes and increse the value by 1. So when I enter a new record there it shows null as in StudentID not showing 2 as per for 2nd record. I want the value of this field (StudentID) will increse from last record (that is 1 ) to 2 in next record. Waiting your reply...

  • Depending on the application you are using to access the data, it won't generate the ID value until you actually submit the data to the database.

    Generally, that means clicking on a "Save" or "Submit" or "Ok" button, or hitting the "Enter" key on your keyboard once you type in the data.

    Once you've done that, it will assign the ID. It can't assign it till the data is submitted to the database.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • with MySelect as

    (

    SELECT Name,

    RANK() over(order by UserId) as newUseId

    from TableUsers

    )

    -- Inserts

    SELECT newUseId,

    'INSERT TableUsers( ... ) VALUES ( '''+ UserId + ''',' + '...)' as SQL1

    FROM MySelect

  • hello you are right,

    when insert a new recordand press tab to new rows then StudentID is automatically changed from null to 2 for 2nd record.

    Many many thanks for giving a such valuable time for my post.

  • You're welcome.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

Viewing 7 posts - 1 through 6 (of 6 total)

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