Change Identity Property in T-SQL

  • How do you change the Identity Property in T-SQL?

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • You can use ALTER TABLE command.

    ---------------------------------------------------
    "Thare are only 10 types of people in the world:
    Those who understand binary, and those who don't."

  • What do you mean?

    Change the column that is an IDENTITY?

    Add or remove an IDENTITY column?

    Change the seed?

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

  • Sean Pearce (5/20/2014)


    What do you mean?

    Change the column that is an IDENTITY?

    Add or remove an IDENTITY column?

    Change the seed?

    There is an Identity Property for a column it is set to True or False.

    You can't change it from falsetto true.

    You can change it in design mode from True to false but how do you do this in T-SQL Code.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • You can't turn an existing IDENTITY off and you can't make an existing column IDENTITY. However you can add an IDENTITY column to an existing table.

    Any indexes should be scripted, dropped and recreated after the change.

    -- Setup test table

    CREATE TABLE TestIdentity

    (ID INT IDENTITY(1, 1),

    Value INT);

    GO

    -- Setup test data

    INSERT INTO TestIdentity (Value)

    VALUES (1),(2),(3),(4),(5);

    GO

    -- Add new column

    ALTER TABLE TestIdentity

    ADD ID2 INT NULL;

    GO

    -- Copy data

    UPDATE TestIdentity

    SET ID2 = ID;

    GO

    -- Remove IDENTITY

    ALTER TABLE TestIdentity

    DROP COLUMN ID;

    GO

    -- Rename new column

    EXEC sp_rename 'TestIdentity.ID2', 'ID', 'COLUMN';

    GO

    INSERT INTO TestIdentity (Value)

    VALUES (11),(12),(13),(14),(15);

    GO

    SELECT * FROM TestIdentity;

    GO

    DROP TABLE TestIdentity;

    GO

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

  • You are right you can't set the Identity Property to true.

    You can set the identity property to false by simply going into design mode and set the property to false.

    I just wanted to know how to do it in T-SQL.

    I guess I could find out how to do so by setting up a trace.

    Thank you.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Welsh Corgi (5/20/2014)


    You are right you can't set the Identity Property to true.

    You can set the identity property to false by simply going into design mode and set the property to false.

    I just wanted to know how to do it in T-SQL.

    I guess I could find out how to do so by setting up a trace.

    Thank you.

    SSMS will create a new table, copy the data across, delete your original table and rename the new one.

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

  • Where do you see the identity property for a column? I did not see it under sp_columns.

    ----------------------------------------------------

  • Sean Pearce (5/21/2014)


    Welsh Corgi (5/20/2014)


    You are right you can't set the Identity Property to true.

    You can set the identity property to false by simply going into design mode and set the property to false.

    I just wanted to know how to do it in T-SQL.

    I guess I could find out how to do so by setting up a trace.

    Thank you.

    SSMS will create a new table, copy the data across, delete your original table and rename the new one.

    I did not see the command to change the column to an identity in you example?

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Welsh Corgi (6/28/2014)


    Sean Pearce (5/21/2014)


    Welsh Corgi (5/20/2014)


    You are right you can't set the Identity Property to true.

    You can set the identity property to false by simply going into design mode and set the property to false.

    I just wanted to know how to do it in T-SQL.

    I guess I could find out how to do so by setting up a trace.

    Thank you.

    SSMS will create a new table, copy the data across, delete your original table and rename the new one.

    I did not see the command to change the column to an identity in you example?

    There isn't one.

    CREATE TABLE NewTable (ID INT IDENTITY(1, 1));

    GO

    SET IDENTITY_INSERT NewTable ON;

    GO

    INSERT INTO NewTable (ID)

    SELECT * FROM OldTable;

    GO

    SET IDENTITY_INSERT NewTable OFF;

    GO

    DROP TABLE OldTable;

    GO

    EXEC sp_rename 'NewTable', 'OldTable';

    GO

    And Vice Versa

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

  • Sean Pearce (6/29/2014)


    Welsh Corgi (6/28/2014)


    Sean Pearce (5/21/2014)


    Welsh Corgi (5/20/2014)


    You are right you can't set the Identity Property to true.

    You can set the identity property to false by simply going into design mode and set the property to false.

    I just wanted to know how to do it in T-SQL.

    I guess I could find out how to do so by setting up a trace.

    Thank you.

    SSMS will create a new table, copy the data across, delete your original table and rename the new one.

    I did not see the command to change the column to an identity in you example?

    There isn't one.

    CREATE TABLE NewTable (ID INT IDENTITY(1, 1));

    GO

    SET IDENTITY_INSERT NewTable ON;

    GO

    INSERT INTO NewTable (ID)

    SELECT * FROM OldTable;

    GO

    SET IDENTITY_INSERT NewTable OFF;

    GO

    DROP TABLE OldTable;

    GO

    EXEC sp_rename 'NewTable', 'OldTable';

    GO

    And Vice Versa

    ok, thank you:-)

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Viewing 11 posts - 1 through 10 (of 10 total)

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