Insert all columns except for Identity ID

  • Two tables A and B have the same structure and the same number of columns.

    First column in both tables is IDENTITY ID.

    The script below cause an error because ID.

    INSERT INTO B

    SELECT *  FROM A

    How to use "Except" to not select column "ID" from table A?

  • There is no Except. You need to name your columns.

    INSERT B (Col1, Col2)
    SELECT Col1, Col2
    FROM A

    • This reply was modified 3 years, 9 months ago by  Phil Parkin.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • To add to Phil's correct answer, if there are a lot of columns, open the Object Explorer window, locate the table you need the column names for, find the "columns" folder, click-hold-and-drag it onto the SSMS editor.  That will pull all of the column names over and then you can just delete the column name(s) you don't need.

     

    --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.


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

  • I'd also ask what is the purpose for copying the same data to another table but with a new ID?

    Far away is close at hand in the images of elsewhere.
    Anon.

Viewing 4 posts - 1 through 3 (of 3 total)

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