Turning IDENTITY_INSERT On and Off in SQL Server 2008
Turning IDENTITY_INSERT On and Off in SQL Server 2008
Understanding IDENTITY_INSERT
In SQL Server 2008, the IDENTITY
property of a column automatically assigns a unique, sequential number to each new row inserted into the table. This is often used for primary key columns. However, there might be situations where you need to manually specify the value for an IDENTITY
column, such as when importing data from another system.
Turning IDENTITY_INSERT On
To temporarily override the automatic IDENTITY
generation and allow you to manually specify values for an IDENTITY
column, you use the SET IDENTITY_INSERT
statement. Here's the syntax:
SET IDENTITY_INSERT [schema_name].[table_name] ON;
Replace [schema_name].[table_name]
with the actual name of your table.
Example:
SET IDENTITY_INSERT dbo.Customers ON;
This enables you to insert rows into the Customers
table, specifying the value for the CustomerID
column.
Inserting Rows with Manually Specified Values
Once IDENTITY_INSERT
is turned on, you can insert rows using the INSERT
statement, specifying the value for the IDENTITY
column:
INSERT INTO dbo.Customers (CustomerID, CustomerName, ...)
VALUES (1000, 'John Doe', ...);
Turning IDENTITY_INSERT Off
After you've finished inserting rows with manually specified values, you should turn off IDENTITY_INSERT
to resume automatic generation:
SET IDENTITY_INSERT [schema_name].[table_name] OFF;
SET IDENTITY_INSERT dbo.Customers OFF;
Important Considerations:
- Scoped to the Table: The
SET IDENTITY_INSERT
statement applies only to the specified table. - Temporary: The setting is temporary and is reset when the connection is closed.
- Potential Data Integrity Issues: If you're not careful, turning off
IDENTITY_INSERT
without inserting the correctIDENTITY
values can lead to data integrity issues.
Use Cases:
- Data Migration: When importing data from a system that doesn't use
IDENTITY
columns. - Testing and Debugging: For testing purposes or to correct data errors.
Creating a Sample Table
Let's first create a sample table with an IDENTITY
column:
CREATE TABLE Customers (
CustomerID INT IDENTITY(1,1) PRIMARY KEY,
CustomerName VARCHAR(50)
);
Turning IDENTITY_INSERT On and Inserting a Row
SET IDENTITY_INSERT dbo.Customers ON;
INSERT INTO dbo.Customers (CustomerID, CustomerName)
VALUES (1001, 'John Doe');
SET IDENTITY_INSERT dbo.Customers OFF;
In this example:
- We enable
IDENTITY_INSERT
for theCustomers
table. - We insert a row with a manually specified
CustomerID
of 1001. - We disable
IDENTITY_INSERT
to resume automatic generation.
SET IDENTITY_INSERT dbo.Customers ON;
INSERT INTO dbo.Customers (CustomerID, CustomerName)
VALUES
(1002, 'Jane Smith'),
(1003, 'Michael Johnson');
SET IDENTITY_INSERT dbo.Customers OFF;
Here, we insert multiple rows with manually specified CustomerID
values in a single INSERT
statement.
Turning IDENTITY_INSERT On and Inserting Rows from a Temporary Table
-- Create a temporary table
CREATE TABLE #TempCustomers (
CustomerID INT,
CustomerName VARCHAR(50)
);
-- Insert data into the temporary table
INSERT INTO #TempCustomers (CustomerID, CustomerName)
VALUES
(1004, 'Emily Davis'),
(1005, 'David Lee');
-- Turn on IDENTITY_INSERT and insert rows from the temporary table
SET IDENTITY_INSERT dbo.Customers ON;
INSERT INTO dbo.Customers (CustomerID, CustomerName)
SELECT CustomerID, CustomerName
FROM #TempCustomers;
SET IDENTITY_INSERT dbo.Customers OFF;
-- Drop the temporary table
DROP TABLE #TempCustomers;
In this example, we use a temporary table to store the data before inserting it into the main table with IDENTITY_INSERT
enabled.
Using a Sequence Object
- Introduction: SQL Server 2012 introduced sequence objects, which provide a more flexible and efficient way to generate sequential numbers.
- Steps:
- Create a sequence object:
CREATE SEQUENCE dbo.MySequence START WITH 1 INCREMENT BY 1 CACHE 1000 MINVALUE 1 MAXVALUE 2147483647;
- Use the sequence in your
INSERT
statements:INSERT INTO dbo.Customers (CustomerID, CustomerName) VALUES (NEXT VALUE FOR dbo.MySequence, 'New Customer');
- Create a sequence object:
Using a Trigger
- Introduction: A trigger can be used to intercept
INSERT
statements and assign a specific value to theIDENTITY
column. - Steps:
- Insert data into the table as usual.
Using a Stored Procedure
- Introduction: A stored procedure can encapsulate the logic for assigning values to the
IDENTITY
column. - Steps:
- Call the stored procedure to insert data:
EXEC sp_InsertCustomer 'New Customer';
- Call the stored procedure to insert data:
Choosing the Right Method:
- Sequence Objects: Generally preferred for their flexibility, efficiency, and scalability.
- Triggers: Useful when you need to perform additional actions before or after inserting data.
- Stored Procedures: Can provide a more encapsulated and reusable approach.
sql sql-server-2008