How to Insert Values to Identity Column in SQL Server?

Override auto-generated IDs when you need to insert specific values manually.

Columns defined as IDENTITY in SQL Server auto-generate their values, so by default you can't insert a specific number into them. But there are legitimate situations — migrating data, restoring specific IDs, or fixing gaps — where you need to insert an explicit value.

The Problem

INSERT INTO Employees (EmployeeID, Name) VALUES (105, 'Riya Sharma');
-- Error: Cannot insert explicit value for identity column
-- unless IDENTITY_INSERT is set to ON.

The Solution: SET IDENTITY_INSERT

SET IDENTITY_INSERT Employees ON;

INSERT INTO Employees (EmployeeID, Name)
VALUES (105, 'Riya Sharma');

SET IDENTITY_INSERT Employees OFF;

Important Rules to Remember

  • You must explicitly list the column names in the INSERT statement
  • Only one table per session can have IDENTITY_INSERT set to ON at a time
  • Always turn it back OFF immediately after the insert to avoid conflicts
💡 Tip: After manually inserting a high identity value, future auto-generated IDs will continue counting up from it — so plan your numbering to avoid collisions.

Common Scenarios

This technique is commonly used when migrating historical data between environments, restoring specific records after an accidental deletion, or keeping IDs consistent across a staging and production database.

Used carefully, SET IDENTITY_INSERT gives you full control over identity values without needing to redesign your schema.

Copying Rows with Their Original IDs

The most common real use is moving rows from one table to another while keeping their IDs, so related tables still match. Wrapping the steps in a transaction keeps the switch from being left on if something fails:

BEGIN TRY
    BEGIN TRANSACTION;

    SET IDENTITY_INSERT dbo.Employees ON;

    INSERT INTO dbo.Employees (EmployeeID, Name, DeptID)
    SELECT EmployeeID, Name, DeptID
    FROM   staging.Employees_Import;

    SET IDENTITY_INSERT dbo.Employees OFF;

    COMMIT TRANSACTION;
END TRY
BEGIN CATCH
    IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION;
    SET IDENTITY_INSERT dbo.Employees OFF;
    THROW;
END CATCH;

Notice the explicit column list in both the INSERT and SELECT. SELECT * will not work while identity insert is on.

Checking and Resetting the Identity Seed

After inserting explicit values, look at what the next generated value will be. DBCC CHECKIDENT reports the current identity value and corrects it if needed:

-- report the current identity value without changing it
DBCC CHECKIDENT ('dbo.Employees', NORESEED);

-- set the current value so the next insert gets 1001
DBCC CHECKIDENT ('dbo.Employees', RESEED, 1000);

Be careful with RESEED. If you set the value below an ID that already exists, a later insert can collide with it and fail on the primary key. Also note that TRUNCATE TABLE resets the identity to its original seed, while DELETE does not.

To find out what ID was just generated in normal inserts, use SCOPE_IDENTITY(), which returns the last identity value created in the current scope. It is safer than @@IDENTITY, which can return a value created by a trigger on another table.

Permissions and Alternatives

Setting identity insert needs elevated rights on the table; typically the user must own the table or have ALTER permission on it. If your application regularly needs to choose its own IDs, identity insert is probably the wrong tool. Consider these options instead:

OptionWhen It Fits
Plain INT column with your own valuesIDs always come from an outside system
SEQUENCE objectYou want generated numbers but need to fetch them before inserting, or share them across tables
UNIQUEIDENTIFIER (GUID)Rows are created in several places and later merged
IDENTITY with IDENTITY_INSERTOccasional migrations, repairs and data restores

Common Mistakes

  • Forgetting the column list, which produces an error even with identity insert on.
  • Leaving identity insert on and then trying to enable it on a second table in the same session, which fails.
  • Expecting identity values to be gap-free. Rolled-back inserts and server restarts can skip numbers, and that is normal behaviour.
  • Running migrations on production without first testing the script on a copy of the data.

Note: these commands are specific to SQL Server. MySQL handles this differently: an AUTO_INCREMENT column accepts an explicit value directly in the INSERT, and the counter moves past it automatically.

Ready to go beyond the basics?

Get hands-on training, live mentorship, and placement support with Uncodemy's Data Analytics Course.

Explore Data Analytics Course →