views:

687

answers:

3

I have below error when i execute the following script;

Could you specify what it is and how it can be resolved?

Insert table(OperationID,OpDescription,FilterID)
values (20,'Hierachy Update',1)


Server: Msg 544, Level 16, State 1, Line 1

Cannot insert explicit value for identity column in table 'table' when IDENTITY_INSERT is set to OFF.
+9  A: 

You're inserting values for OperationId that is an identity column.

You can turn on identity insert on the table like this so that you can specify your own identity values.

SET IDENTITY_INSERT Table1 ON

INSERT INTO TABLE1 (OperationID,OpDescription,FilterID) VALUES (20,'Hierachy Update',1)

SET IDENTITY_INSERT Table1 OFF
pjp
+1 exactly - turn the option to allow explicit inserts **ON**, then insert, then turn the option **OFF** again
marc_s
If you really want to add your value to the identity column, this is your answer, but on the other hand, someone have set the column to be incrementing by itself up on insert. In that case the table will keep track of the next free number, and you do not need to generate the OperationID by yourself. The new id can be fetched by SELECT SCOPE_IDENTITY().
Hakan Winther
+1  A: 

don't put value to OperationID because it will be automatically generated. try this:

Insert table(OpDescription,FilterID) values ('Hierachy Update',1)
Wael Dalloul
+1  A: 

Be very wary of setting IDENTITY_INSERT Table1 ON. This is a poor practice unless the datbase is in maintenance mode and set to single user. This affects not only your insert but those of anyone else trying to access the table.

Why are you trying to put a value into an indentiy field?

HLGEM