How to turn IDENTITY_INSERT on and off using SQL Server 2008?
To retain values in an SQL Server table's identity column, you need to sequence commands. Turn SET IDENTITY_INSERT ON
before the insert operation. After inserting, turn it back to SET IDENTITY_INSERT OFF
. Remember, SQL Server only extends the hospitality of IDENTITY_INSERT
to one table at a time.
Quick steps:
Pre-insert preparations
Before you jump into SET IDENTITY_INSERT
action, tick off these boxes:
- Verify there's an identity column in your table.
- There should not be any other table hosting a
IDENTITY_INSERT
party in the current session. - When you're the DJ, you need to have all the tracks. Be ready with data for all non-nullable columns.
- You don't want to hear the same track twice. Ensure explicit identity values are unique and don't exceed the column's range.
Step-by-step usage
SET IDENTITY_INSERT
is no rocket science, but you need to follow a sequence:
-
Start the party - Activate
IDENTITY_INSERT
: Let the user take over the identity column DJ console. -
Spin your tracks - Insert the data: Time to play your tracks. Insert operation with explicit values for the identity column.
-
Hand it back - Deactivate
IDENTITY_INSERT
: Time to return DJ console. Turn off identity inserts.
All your insert columns need to match the table's columns to avoid any compilation concerts.
Avoid stumbling blocks
Be cautious of few gotchas:
- Scope: Setting
IDENTITY_INSERT
ON for a rocking party? Remember ending it in the same session is crucial. - Permissions: You need an exclusive VIP pass (alter table permissions) to start the
IDENTITY_INSERT
party. - Track Limits: Your tracks (explicit identity values) must meet the column's type requirements.
- Repeated tracks: Ensure you're not repeating the tracks (identity values), as they can lead to constraint boogie nights.
- Pre-party call: Before you start the
IDENTITY_INSERT
party, ensure to announce your party (setIDENTITY_INSERT ON
) in the same session.
Coding with IDENTITY_INSERT
If you're teaming up IDENTITY_INSERT
with scripts or applications, remember few things:
- Validation: Don't let two
IDENTITY_INSERT
party overlap. Check if the previous one is done. - Execution: Make
.ExecuteNonQuery()
your bouncer to take care of any unwelcome issues. - Transaction: Treat
IDENTITY_INSERT
setting andINSERT
statement as an exclusive party to ensure consistency. - Error prevention: Place
IDENTITY_INSERT
in a nicetry / catch
lounge for party-cleanups usingSET IDENTITY_INSERT OFF
in thefinally
block.
Bulk insert operation
For large VIP parties (bulk operations), these are a must:
- Batch Processing: It's not practical to individually welcome every guest. Group your
INSERT ... VALUES ...
statements. - Bulk Insert: Let the bouncers (
BULK INSERT
orOPENROWSET
) deal with guest-list (data files). - Minimal Logging: Large parties can be messy (heavy transaction log space), ensure you hire cleaning staff (minimal logging) for swift cleaning.
Was this article helpful?