Identity insertion and updation

  • balaji_rcs

    Mr or Mrs. 500

    Points: 570

    I have a lot of tables with identity column. I want to remove the identity properties of all the tables while inserting and updating the tables.

  • Carolyn Richardson

    SSCrazy Eights

    Points: 8352

    See SET IDENTITY_INSERT in BOL

    Facts are stubborn things, but statistics are more pliable - Mark Twain
    Carolyn
    SQLServerSpecialists[/url]

  • Johan Bijnens

    SSC Guru

    Points: 134282

    - Maybe creating a DTS package to load your tables may be the way to go, because it has the option "keep identities"

    -If using set identity_insert on, keep in mind you have to list all columns in your insert statement.

    e.g.

    set identity_insert on

    insert into mytable(col1,col2,col333,...)

    select * from mysourcetable

    set identity_insert off

    dbcc checkident('mytable') -- check bol

    Johan


    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt ?

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me[/url] :alien: but most of the time this is me :hehe:

  • balaji_rcs

    Mr or Mrs. 500

    Points: 570

    Hi,

    Thanks for your reply. I want to remove the identity properties in the table totally. i.e I want to remove the identity while update also.

  • Johan Bijnens

    SSC Guru

    Points: 134282

    if you want to completely get rid or the usage of the identity property, you'll have to generate drop/create table statements !!

    Keep in mind, that there is a purpose for identity ! (automatic increment at insert time.)

    Johan


    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt ?

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me[/url] :alien: but most of the time this is me :hehe:

  • Johan Bijnens

    SSC Guru

    Points: 134282

    I've just received this article and thought it might also help you :

    http://www.mssqltips.com/tip.asp?tip=1397

    Johan


    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt ?

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me[/url] :alien: but most of the time this is me :hehe:

  • balaji_rcs

    Mr or Mrs. 500

    Points: 570

    Thanks for your reply. I've nearly 700 tables, and more than 500 tables containing identity column and i want to remove the identity property without dropping/creating the table.

  • Kishore.P

    SSCrazy Eights

    Points: 8147

    use sp_msforeachtable to drop the identity column in all tables.

  • Kishore.P

    SSCrazy Eights

    Points: 8147

    use sp_msforeachtable to drop the identity column in all tables.

  • balaji_rcs

    Mr or Mrs. 500

    Points: 570

    Hi Kishore,

    Thanks for your reply. Let me know how to use this procedure for removing the identity column of all tables. And can you tell me about this procedure.

  • Greg Charles

    SSC-Forever

    Points: 45403

    I'm a little confused. Early in the thread it sounded like you want to remove the identity property and retain the data, but your last post sounds like you are okay with dropping the columns with identity property from the tables.

    Are you ultimately wanting to have 500 tables with no identity columns?

    Greg

    Greg

  • Jeff Moden

    SSC Guru

    Points: 996676

    balaji_rcs (12/24/2007)


    Hi,

    Thanks for your reply. I want to remove the identity properties in the table totally. i.e I want to remove the identity while update also.

    Yeah, uh-huh... what are you going to use to replace the auto-numbering property of the IDENTITY property? Sequence table? Better post that code so we can check it or you'll end up with a bazillion deadlocks per day. SELECT MAX()? Again, better post that code so we can make sure that you won't end up with dupe ID's on a high usage system.

    And, then, you might as well convert all of your code to bloody cursors because you're gonna have a hell of a time inserting more than one row at a time no matter which of those two methods you use. If you have a third method not offered above, please post it... I want to see what type of knife you're falling on 😉

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".
    "If "pre-optimization" is the root of all evil, then what does the resulting no optimization lead to?"

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden

    SSC Guru

    Points: 996676

    Kishore.P (1/8/2008)


    use sp_msforeachtable to drop the identity column in all tables.

    Yup... easy to do if no foreign keys... of course, if these IDENTITY columns are being used as the PK for tables, your whole database becomes a useless piece of slag.

    C'mon folks! Doesn't anyone else see a problem with this request??? :blink:

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".
    "If "pre-optimization" is the root of all evil, then what does the resulting no optimization lead to?"

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • G.R.Prithiviraj Kulasingham

    SSCarpal Tunnel

    Points: 4104

    Removing identity property is something I have seen many people doing for some reason.

    Most of the time, they perform an additional query to get the next value and then pass it. This method creates additional locks and will not work perfectly in a multi user environment.

    I believe identity is one of the good things SQL Server has.

    Having said that, there is another way of moving them. It will work better if you do not have foreign keys defined against this table and this table is large.

    As of now, it will work only in enterprise edition of SQL Server 2005, but I believe it will work in some additional editions of SQL Server 2008

    1. Create another table with the same structure except the identity property.

    2. Switch the data from old table to new table

    3. Drop the old table (which has the identity property)

    4. Rename the new table with proper name.

    In case you have soem foreign keys you need to drop the foreign keys and re-create them.

    I heard that SQL Server mobile edition has a command where you can simply turn off the identity property, but it is not yet available on other editions.

    Cheers,
    Prithiviraj Kulasingham

    http://preethiviraj.blogspot.com/

  • G.R.Prithiviraj Kulasingham

    SSCarpal Tunnel

    Points: 4104

    Oops I just realized that this is quite old thread. I am not sure why it suddenly came into my screen.

    Cheers,
    Prithiviraj Kulasingham

    http://preethiviraj.blogspot.com/

Viewing 15 posts - 1 through 15 (of 15 total)

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