Enable IDENTITY_INSERT SQL Server 2008: Fix Insert Error
Learn how to properly set IDENTITY_INSERT ON/OFF in SQL Server 2008 to avoid 'Cannot insert explicit value for identity column' error. SSMS guide, app code examples, session-specific tips, and troubleshooting for Baskets table.
How to properly enable and disable IDENTITY_INSERT in SQL Server 2008 to avoid ‘Cannot insert explicit value for identity column’ error?
I ran the following query:
SET IDENTITY_INSERT Database.dbo.Baskets ON
It completed successfully, but my application still fails with:
Cannot insert explicit value for identity column in table 'Baskets' when IDENTITY_INSERT is set to OFF.
How do I turn IDENTITY_INSERT ON correctly in SQL Server 2008, possibly using SQL Server Management Studio? Why isn’t the setting persisting for my application?
The “Cannot insert explicit value for identity column” error in SQL Server 2008 strikes because IDENTITY_INSERT is strictly session-specific—your SSMS command only affects that one connection, not your application’s separate session. To properly enable IDENTITY_INSERT for table ‘Baskets’, run SET IDENTITY_INSERT Database.dbo.Baskets ON immediately before your INSERT statement in the exact same session, then disable it with OFF right after. This ensures explicit values go into the identity column without glitches, and it’s the same reliable behavior in SQL Server 2008 as today.
Contents
- What is IDENTITY_INSERT in SQL Server 2008?
- Correct Syntax to Set IDENTITY_INSERT ON and OFF
- Why Your SSMS SET IDENTITY_INSERT ON Isn’t Working for the Application
- Step-by-Step Guide: Enabling IDENTITY_INSERT in SQL Server Management Studio
- IDENTITY_INSERT in Application Code: Session-Specific Best Practices
- Common Errors, Permissions, and Troubleshooting Tips
- Sources
- Conclusion
What is IDENTITY_INSERT in SQL Server 2008?
Ever tried shoving a specific ID into an auto-incrementing column, only to get slapped with that pesky error? That’s where IDENTITY_INSERT comes in. In SQL Server 2008, identity columns (those with IDENTITY(1,1) properties) normally generate their own sequential values—you can’t just INSERT an explicit number like 999 without permission.
But flip the switch with SET IDENTITY_INSERT ON, and suddenly you control the values. Why bother? Common scenarios: migrating data, fixing gaps in sequences, or syncing tables across databases. According to the official SQL Server documentation, this setting lets you override the auto-generation temporarily.
Key catch? It’s not global. It sticks to your session only. And SQL Server 2008 handles it the same as modern versions—no changes there, even though the edition reached end-of-life years ago. Think of it as a polite “one table at a time” rule per connection.
Correct Syntax to Set IDENTITY_INSERT ON and OFF
Getting the syntax wrong? That’s a fast track to frustration. Here’s the precise T‑SQL for your ‘Baskets’ table:
SET IDENTITY_INSERT Database.dbo.Baskets ON;
-- Now your INSERT with explicit identity value
INSERT INTO Database.dbo.Baskets (BasketID, BasketName, CreatedDate)
VALUES (999, 'Special Basket', GETDATE());
SET IDENTITY_INSERT Database.dbo.Baskets OFF;
See that sequence? ON first, then INSERT (include the identity column in your column list and VALUES), then OFF. Miss the column list? Boom—error.
From DBA Stack Exchange discussions, only one table gets this privilege per session. Try two? You’ll hit “IDENTITY_INSERT is already ON for table ‘OtherTable’.” Always qualify the table fully ([database].[schema].[table]) to avoid ambiguity, especially in multi‑db setups.
And pro tip: Wrap it in a transaction if you’re scripting bulk inserts. It rolls back cleanly if something flakes out.
BEGIN TRANSACTION;
SET IDENTITY_INSERT Database.dbo.Baskets ON;
-- Multiple INSERTs here
SET IDENTITY_INSERT Database.dbo.Baskets OFF;
COMMIT TRANSACTION;
Short and sweet. No dynamic SQL needed unless you’re building table names on the fly.
Why Your SSMS SET IDENTITY_INSERT ON Isn’t Working for the Application
You ran that query in SSMS, it said “Command(s) completed successfully,” but your app chokes. Sound familiar? Here’s the culprit: sessions.
SSMS opens a new connection per tab—or even per query window if you’re not careful. Your app? Totally separate connection pool, oblivious to your manual tweak. Stack Overflow threads on this exact issue nail it: IDENTITY_INSERT activates at execution time, scoped to that connection alone. No persistence across sessions. Ever wonder why it feels like it vanished? Because it did—for everyone else.
Your app likely uses ADO.NET, Entity Framework, or some ORM with its own connection string. That SSMS session is like a solo coffee run; the app’s brewing its own pot. Bottom line: the setting doesn’t “stick” globally in SQL Server 2008. You gotta embed it in the code path hitting ‘Baskets’.
Quick test? Fire up a new SSMS tab, run your app’s INSERT query verbatim. Fails? Proof positive.
Step-by-Step Guide: Enabling IDENTITY_INSERT in SQL Server Management Studio
SSMS is your playground for testing. Let’s nail this for SQL Server 2008.
-
Open a fresh query window: File > New > Query. One tab, one session—don’t multitask.
-
Paste and execute the full batch:
USE Database; -- Switch to your DB
SET IDENTITY_INSERT dbo.Baskets ON;
INSERT INTO dbo.Baskets (BasketID, OtherColumn) VALUES (123, 'Test');
SET IDENTITY_INSERT dbo.Baskets OFF;
SELECT * FROM dbo.Baskets WHERE BasketID = 123; -- Verify
-
Hit F5. Watch for green checkmarks. If it gripes about permissions, more on that later.
-
Check session isolation: Open another tab, try an INSERT with explicit ID. Error! That’s your demo of session limits.
SSMS Query Options (Ctrl+K > Advanced) won’t help here—it’s all connection‑level. For SQL Server 2008, no GUI toggle exists; pure T‑SQL. Run this to peek at your session:
SELECT @@SPID AS SessionID;
Match it against app logs if they’re verbose.
Done right, you’ll insert that explicit value without a hitch. Now replicate in your app.
IDENTITY_INSERT in Application Code: Session‑Specific Best Practices
Apps demand repeatability. Ditch manual SSMS fiddling—bake it into code.
Take C# with SqlConnection:
using (var conn = new SqlConnection(connectionString))
{
conn.Open();
using (var cmd = new SqlCommand("SET IDENTITY_INSERT Database.dbo.Baskets ON", conn))
cmd.ExecuteNonQuery();
// Your INSERT
using (var insertCmd = new SqlCommand(
"INSERT INTO Database.dbo.Baskets (BasketID, BasketName) VALUES (@id, @name)", conn))
{
insertCmd.Parameters.AddWithValue("@id", 999);
insertCmd.Parameters.AddWithValue("@name", "App Basket");
insertCmd.ExecuteNonQuery();
}
using (var cmd = new SqlCommand("SET IDENTITY_INSERT Database.dbo.Baskets OFF", conn))
cmd.ExecuteNonQuery();
}
Same connection, same session. Entity Framework? Override SaveChanges or use raw SQL. MSSQLTips examples show it for data syncs—perfect for your case.
Best practices?
- Always OFF after—blocks normal inserts otherwise.
- One table/session: Cycle if multi‑table.
- Connection pooling? Settings reset per command batch, so embed per operation.
- Log @@SPID to confirm.
Your app fails because it skips the ON step. Add it, problem solved.
Common Errors, Permissions, and Troubleshooting Tips
That error message screams “OFF.” Other gotchas?
- “IDENTITY_INSERT already ON”: Another table owns the slot. SET OFF first.
- Permissions denied: Needs
ALTERon table or db_owner/sysadmin. Test:
SELECT HAS_PERMS_BY_NAME('Database.dbo.Baskets', 'OBJECT', 'ALTER');
- Parse vs. execute: Settings apply at runtime—deferred in stored procs.
- No column list: INSERT must specify identity column explicitly.
From SQL Server docs, explicit values require the column named. Troubleshooting flow: Check SPID match, permissions, then syntax.
App still busted? Profiler or Extended Events (SQL 2008 has basics) to trace the exact failing query.
Sources
- SET IDENTITY_INSERT (Transact‑SQL) — Official syntax, rules, and error details for SQL Server: https://learn.microsoft.com/en-us/sql/t-sql/statements/set-identity-insert-transact-sql?view=sql-server-ver17
- How to turn IDENTITY_INSERT on and off using SQL Server 2008? — Practical SSMS and app session troubleshooting: https://stackoverflow.com/questions/7063501/how-to-turn-identity-insert-on-and-off-using-sql-server-2008
- Why is IDENTITY_INSERT ON only allowed on one table at a time? — Explains session and one‑table limits: https://dba.stackexchange.com/questions/12650/why-is-identity-insert-on-only-allowed-on-one-table-at-a-time
- SQL Server IDENTITY Insert to Keep Tables Synchronized — Code examples for app integration and syncing: https://www.mssqltips.com/sqlservertip/1061/sql-server-identity-insert-to-keep-tables-synchronized/
Conclusion
Mastering IDENTITY_INSERT in SQL Server 2008 boils down to session awareness: enable it ON before INSERT, disable OFF after, all in the same connection—whether SSMS testing or app code. Skip that, and “Cannot insert explicit value for identity column” haunts you forever. Test small, embed in your app, and you’re golden. Questions on your specific stack? Dive into those logs.