Results 1 to 6 of 6

Thread: Identity_insert ?/???

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    35

    Identity_insert ?/???

    When I try using the "insert into select from"....for some of my tables I get this error:

    An explicit value for the identity column in table 'db1' can only be specified when a column list is used and IDENTITY_INSERT is ON

    i got some help frm google.
    SET IDENTITY_INSERT YourTable ON

    INSERT INTO YourTable
    SELECT * FROM YourOtherTable

    SET IDENTITY_INSERT YourTable OFF


    i used it bt again error comes as syntax error for both lines ON/OFF
    wht does it mean by" IDENTITY_INSERT is ON"


    wht to do?
    thanks in advance

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Identity_insert ?/???

    Quote Originally Posted by ash007
    When I try using the "insert into select from"....for some of my tables I get this error:

    An explicit value for the identity column in table 'db1' can only be specified when a column list is used and IDENTITY_INSERT is ON

    i got some help frm google.
    SET IDENTITY_INSERT YourTable ON

    INSERT INTO YourTable
    SELECT * FROM YourOtherTable

    SET IDENTITY_INSERT YourTable OFF


    i used it bt again error comes as syntax error for both lines ON/OFF
    wht does it mean by" IDENTITY_INSERT is ON"


    wht to do?
    thanks in advance
    The identity column, at least in SQL 2000, is maintained by the database itself. It's "touchy" about people messing with it. A primary use is to for the table to have a unique identifier versus maintaining unique keys programmatically. Normally you don't want to be changing it except for controlled DB maintenance.

    Here is a link for maintaining it outside the scope of the DB.

    http://www.sqlteam.com/article/under...entity-columns

    Without knowing what you are doing I'm thinking your approach is wrong if someone else set up the table and you are just using it. Identity columns are not meant to be overwritten in most cases. It is basically a primary key.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Identity_insert ?/???

    Moved to Database Development

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Identity_insert ?/???

    Unless you have a specific reason to specify the identity value yourself...

    Then simply leave that column out of the INSERT list.

    It might seem odd - but by not specifying that column in the list - it will get a value automatically.

    What are you actually trying to achieve??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: Identity_insert ?/???

    Does the table that you're inserting from have the primary key you need in it already? If so, take the IDENTITY property off of the table that you're inserting into. That's there simply to create a unique primary key for each record in cases where you don't have anything unique to identify each row yourself. If not, don't include the identity column in what's being copied.

    Also, doing a SELECT * in an insert isn't the best programming practice, because if anything changes about either table, your whole application will then bomb. It can be time-consuming to write out each column name, but it makes for far less mantenance headaches in the future.

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2007
    Posts
    35

    Re: Identity_insert ?/???

    Thanks 2 all
    the problem was same ^^

Posting Permissions

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



Click Here to Expand Forum to Full Width