SQL Server Error 545: Explicit Value Must Be Specified
In this article
SQL Server Error 545 occurs when you attempt to manually insert a value into a column defined with the IDENTITY property without first granting the session permission to do so. To fix this, you must execute the command SET IDENTITY_INSERT [TableName] ON before your INSERT statement, and then set it back to OFF once the operation is complete. This error is a safeguard designed to prevent the accidental overwriting of automatically generated primary keys, ensuring the integrity of the sequence.
What You'll Need
- Access to SQL Server Management Studio (SSMS), Azure Data Studio, or any SQL query editor.
- Permissions to modify data in the target table (specifically INSERT permissions).
- Administrative or owner permissions to change the IDENTITY_INSERT setting for the specific table.
- The exact name of the table and the identity column you are targeting.
Step-by-Step
- Identify the Target Table: Determine which table is triggering the error. The error message usually specifies the table name. Ensure you have the exact spelling and schema (e.g.,
dbo.Employees). - Open a New Query Window: Launch your SQL editor and open a new query tab. It is critical that the
SET IDENTITY_INSERTcommand and theINSERTstatement are executed within the same session (the same connection window). If you run the SET command in one window and the INSERT in another, the second window will still trigger Error 545 because the setting is session-scoped. - Enable Identity Insert: Type and execute the following command:
SET IDENTITY_INSERT YourTableName ON;. ReplaceYourTableNamewith the actual name of your table. This tells SQL Server that for the remainder of this specific connection, you intend to provide your own values for the identity column rather than letting the system generate them. - Write the Explicit INSERT Statement: When
IDENTITY_INSERTis ON, you cannot use a shorthand INSERT statement. You must explicitly list the columns you are inserting into, including the identity column. For example:INSERT INTO YourTableName (IdentityColumn, Column2, Column3) VALUES (101, 'Value2', 'Value3');. If you omit the column list, SQL Server will throw a different error because it cannot automatically map the values when identity insertion is enabled. - Execute the Insert: Run the INSERT query. You should now see a message indicating that the row was successfully inserted with your specified ID.
- Disable Identity Insert: Immediately after your data migration or manual correction is finished, run:
SET IDENTITY_INSERT YourTableName OFF;. This restores the default behavior where SQL Server automatically increments the ID. Leaving this ON is a bad practice and can lead to errors in other application processes that expect the system to handle ID generation. - Verify the Data: Run a
SELECT * FROM YourTableName WHERE IdentityColumn = [YourValue];to ensure the record was placed correctly and that the identity seed has updated to reflect the new highest value.
Common Mistakes
One of the most frequent mistakes is attempting to enable IDENTITY_INSERT for multiple tables simultaneously. SQL Server only allows IDENTITY_INSERT to be set to ON for one table at a time per session. If you try to set it to ON for Table B while it is still ON for Table A, the system will automatically switch it off for Table A and enable it for Table B. This can cause scripts that handle multiple tables to fail if the sequence of commands is not strictly managed.
Another common error is forgetting the explicit column list. In a standard INSERT, you might write INSERT INTO Table VALUES (1, 'Data'). However, once IDENTITY_INSERT is ON, this syntax is forbidden. You must specify INSERT INTO Table (ID, Name) VALUES (1, 'Data'). Failing to do this results in a syntax error, even though you have already bypassed the identity restriction.
Users also frequently attempt to run these commands through an application's ORM (Object-Relational Mapper) like Entity Framework or Hibernate without realizing that the ORM often opens and closes connections rapidly. Because SET IDENTITY_INSERT is session-specific, the command may be executed on one connection, but the actual INSERT may be sent over a different connection from the connection pool, leading to the reappearance of Error 545.
Tips
When performing large data migrations where you need to preserve existing IDs from a legacy system, wrap your entire block of code in a transaction. This ensures that if an error occurs halfway through the process, you can roll back the changes and avoid leaving the IDENTITY_INSERT setting in an inconsistent state. Use BEGIN TRANSACTION at the start and COMMIT at the end.
If you find yourself needing to do this frequently for a specific table during development, consider whether the column actually needs to be an IDENTITY column. If the IDs are coming from an external source and are not generated by SQL Server, removing the IDENTITY property and using a Primary Key constraint instead will eliminate the need for these commands entirely.
To check the current identity value of a table after a manual insert, use the DBCC CHECKIDENT ('TableName', NORESEED) command. This helps you verify if the manual insert has pushed the internal counter forward or if you need to manually reseed the identity to prevent future primary key collisions.
Troubleshooting
If you have executed SET IDENTITY_INSERT [TableName] ON but still receive Error 545, first check your connection. Ensure you are not using a tool that executes each line of a script as a separate batch or session. In SSMS, highlight all the code (the SET ON, the INSERT, and the SET OFF) and press F5 to execute them as a single block.
If you receive an error stating that IDENTITY_INSERT is already ON for another table, you must explicitly set the other table to OFF or simply execute the SET ON command for your current table, which will override the previous one. However, for clarity in scripts, it is best to explicitly turn off the previous table first.
If you encounter a Primary Key violation error after enabling identity insert, it means the value you are trying to force into the table already exists. You must either choose a unique value or delete the existing record before attempting the insert. This is common when importing data from multiple sources that may have overlapping ID ranges.
In cases where you are using stored procedures, remember that the SET IDENTITY_INSERT command must be inside the procedure or executed in the same session as the procedure call. If the procedure creates its own internal connection or uses a different scope, the setting will not carry over.
Frequently Asked Questions
Can I use IDENTITY_INSERT for multiple tables at once?
No, SQL Server allows only one table per session to have IDENTITY_INSERT set to ON. If you enable it for a second table, it is automatically disabled for the first one.
Does this command change the identity seed permanently?
Manual inserts can affect the current identity value. If you insert a value higher than the current seed, SQL Server typically updates the seed to that new value plus the increment. You can use DBCC CHECKIDENT to verify or reset the seed.
Is it possible to insert a NULL into an identity column?
No, identity columns cannot contain NULL values. They are designed to be mandatory, auto-incrementing integers. If you need a nullable column, you cannot use the IDENTITY property.
Why do I have to list the columns explicitly during the insert?
This is a requirement of the SQL Server engine. When IDENTITY_INSERT is enabled, the engine needs absolute certainty about which value is being mapped to the identity column to prevent data misalignment.
Can I use this command in a SELECT INTO statement?
No, SET IDENTITY_INSERT only applies to INSERT statements. If you are using SELECT INTO to create a new table, the identity property is handled differently depending on whether the source table also has an identity column.
What happens if I forget to set IDENTITY_INSERT back to OFF?
The setting only persists for the duration of your current session. Once you close the query window or disconnect from the server, it automatically reverts to OFF. However, it is still best practice to turn it off explicitly in your scripts.