Insert a specific identifier in the identification field

Is it possible to insert a specific value into an identifier field in an SQL table?

I deleted a few lines and imported them again, but I need to insert some exceptions with exact identifiers.

+4
source share
1 answer

You can enable / disable the insertion of identity values ​​using SET IDENTITY_INSERT On / Off :

To enable custom values:

SET IDENTITY_INSERT TableName ON

To enable automatic values:

SET IDENTITY_INSERT TableName OFF

Note that you must explicitly specify columns (non-empty) if you want to insert identification values, for example here:

INSERT INTO TableName (ID, Text, OtherColumns...) Values (99, 'foo', ...)

You cannot omit the column list with this syntax:

INSERT INTO TableName Values (99,'foo')
+7

Source: https://habr.com/ru/post/1654315/


All Articles