Cannot insert explicit value for identity column in table 'table' when IDENTITY_INSERT is set to OFF

前端 未结 22 1165
我在风中等你
我在风中等你 2020-11-22 13:56

I have the below error when I execute the following script. What is the error about, and how it can be resolved?

Insert table(OperationID,OpDescription,Filte         


        
22条回答
  •  清酒与你
    2020-11-22 14:33

    Note that if you are closing each line with ;, the SET IDENTITY_INSERT mytable ON command will not hold for the following lines.

    i.e.
    a query like

    SET IDENTITY_INSERT mytable ON;
    INSERT INTO mytable (VoucherID, name) VALUES (1, 'Cole');
    

    Gives the error
    Cannot insert explicit value for identity column in table 'mytable' when IDENTITY_INSERT is set to OFF.

    But a query like this will work:

    SET IDENTITY_INSERT mytable ON
    INSERT INTO mytable (VoucherID, name) VALUES (1, 'Cole')
    SET IDENTITY_INSERT mytable OFF;
    

    It seems like the SET IDENTITY_INSERT command only holds for a transaction, and the ; will signify the end of a transaction.

提交回复
热议问题