Inserting rows into a table with one IDENTITY column only

前端 未结 3 1074
生来不讨喜
生来不讨喜 2020-12-02 09:35

I have a table Administrator with only one column, adminId which is the primary-key. Because of business rules it has to be this way.

I\'d like to understand once a

3条回答
  •  天命终不由人
    2020-12-02 10:21

    You need to add the IDENTITY_INSERT to your select statement:

    SET IDENTITY_INSERT MyTable ON
    
    INSERT INTO MyTable
    (AdminCol)
    
    SELECT AdminColValue
    
     FROM Tableb
    

    When you're done, make sure you remember to

    SET IDENTITY_INSERT MyTable OFF
    

    Here's a good description of how it works from BOL: http://msdn.microsoft.com/en-us/library/aa259221(SQL.80).aspx

提交回复
热议问题