Home Forums SQL Server 2008 T-SQL (SS2K8) SELECT * INTO Table without propagating IDENTITY attribute? RE: SELECT * INTO Table without propagating IDENTITY attribute?

  • PhilPacha (12/27/2012)


    Using the simplest SELECT INTO syntax, the IDENTITY property of a column is part of the definition of the destination table. Is there a simple way to avoid this? I can't seem to find anything in the documentation, or my google-foo is deficient.

    I'm trying to create an mirror image of a table, using a false WHERE clause to create only the table structure.

    It's easy. Just recast the column as an INT during the SELECT/INTO. If you want it to be a NOT NULL column, just wrap an ISNULL around that...

    SELECT SomeBasicIntColumn = ISNULL(CAST(SomeIdentityColumn AS INT),0),

    other columns

    INTO #SomeTempTable

    FROM dbo.SomeOtherTable

    WHERE 1 = 0

    ;

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