Setting Identity to on or off in SQL server

后端 未结 4 1326
情深已故
情深已故 2021-02-12 18:26

I want to set Is Identity property of a column to off and after inserting an explicit value setting it to on again.I\'ve written this query :

SET IDENTITY_INSERT         


        
相关标签:
4条回答
  • 2021-02-12 18:51

    You actually want to use SET IDENTITY_INSERT Tbl_Cartoons ON before attempting to insert explicit values.

    You're saying that "I am going to be responsible for inserting values into the IDENTITY column".

    SET IDENTITY_INSERT Tbl_Cartoons OFF says "I'll let the system take responsibility for inserting values into the IDENTITY column".

    0 讨论(0)
  • 2021-02-12 18:58

    To insert explicit values into the identity column , do the following :

    SET IDENTITY_INSERT Tbl_Cartoons  ON
    GO
    
    -- code to insert explicit ID values
    
    SET IDENTITY_INSERT Tbl_Cartoons  OFF
    GO
    
    0 讨论(0)
  • 2021-02-12 19:02

    The session that sets SET IDENTITY_INSERT is allowed to enter explicit values.

    But the column is still an identity column. It's just that your session can ignore the identity constraint.

    0 讨论(0)
  • 2021-02-12 19:10

    All the line you've given does is to disable the identity so that you can insert specific values into your identity column - usually this is needed for one-offs such as moving data around. The identity is still there on the column, its just not being acted upon. Conceptually this is similar to the difference between disabling and removing triggers.

    To remove the identity from the column entirely is harder. The question covers it, but the basic idea is that you have to create a new column, copy the data over, then remove the identity column.

    0 讨论(0)
提交回复
热议问题