Dynamically add Ids by 1 in SQL insert.

Posted on 2011-05-10
Last Modified: 2012-06-27
Hello Experts,

I am inserting data into a single table and I would like to do this in bulk. The only thing that is stopping me is one field that does not accept nulls and is an id that I assign to each item. I am trying to create a procedure that will dynamically add this field (i++) instead of me going in one by one.

Is this possible? Do I need to use script?

Question by:tparus
    LVL 15

    Accepted Solution

    if you are using SQL Server, I'm pretty sure that you can change the type of this field to autoincrement and then, do not refer this field on the insert clause.

    Author Comment

    Is there a way of doing this without modifying the table?
    LVL 25

    Assisted Solution

    by:Ron M
    Without modifying the table, you would be facing another issue.

    You could get the "next value" easily...but what if two seperate processes are both looking for the "next value" at the same time ...and get the same value ?

    Even if you did ...MAX(ID) + 1... the same problem remains.

    The best advice is to modify the table to increment the field automatically.

    Author Comment

    Ok, but what if there are two fields? Will MS SQL let me have two fields with Identity turned on?
    LVL 15

    Assisted Solution

    by:Walter Ritzel
    I dont see why not...

    Author Comment


    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now