What's the code in turning an ID into an autonumber data type?

前端 未结 1 1414
粉色の甜心
粉色の甜心 2021-01-16 09:35

I am using a visual studio 2010 and Microsoft SQ\'L Server 2005.

I want my text box Equip_No to be an auto number data type.

I already followed these steps

相关标签:
1条回答
  • 2021-01-16 10:22

    I don't think you can alter an existing column into identity in Sqlserver-2005.

    You can however add a column with identity and after that manipulate the values and change the seed

    Adding the autoincremental column

    ALTER TABLE yourtable 
    ADD newid INT identity(1,1)
    

    Making it possible to change your values in your id column however you want

    SET IDENTITY_INSERT yourtable ON
    

    Do your manipulating here, example:

    UPDATE yourtable SET newid = id
    

    Cleanup

    SET IDENTITY_INSERT yourtable OFF
    
    DBCC CHECKIDENT
     ('YourTable', RESEED, <a value at least 1 higher than the max existing value>);
    

    Replacing old column

    ALTER TABLE yourtable DROP COLUMN id
    SP_RENAME 'yourtable.newid', 'id', 'COLUMN'
    
    0 讨论(0)
提交回复
热议问题