• pawantiwari.3987 (3/18/2011)


    alter column to identity is very easy in ms sql server 2005

    just right click on column you want to alter and click on design and change identity to YES

    Enjoy!!!!!

    Okay now try to get that into a script to be run on a production server.

    FYI this can also be done in SQL2000 Enterprise Manager. But as that and SSMS doesnt do anything magically. They are still restricted to what SQL can do and what can be scripted. Now the script that gets created in both 2000 and 2005 is that a new table is created and all data is copied to it. The old table gets deleted and the new one gets renamed.

    There is more to SQL then point and click (but can be useful for getting a script easy :-D)

    This is part off the script that gets created (on a little test table i created just for the occation)

    CREATE TABLE dbo.Tmp_tbl

    (

    i int NOT NULL IDENTITY (1, 1)

    ) ON [PRIMARY]

    GO

    SET IDENTITY_INSERT dbo.Tmp_tbl ON

    GO

    IF EXISTS(SELECT * FROM dbo.tbl)

    EXEC('INSERT INTO dbo.Tmp_tbl (i)

    SELECT i FROM dbo.tbl TABLOCKX')

    GO

    SET IDENTITY_INSERT dbo.Tmp_tbl OFF

    GO

    DROP TABLE dbo.tbl

    GO

    EXECUTE sp_rename N'dbo.Tmp_tbl', N'tbl', 'OBJECT'

    GO

    Oh and this would also indicate that you cant add the identity property to an existing column.

    /T