| title | Keep Identity Values When Bulk Importing Data | |||
|---|---|---|---|---|
| description | When you bulk import data that contains identity values to a SQL Server instance, by default, it assigns new values. You can choose to keep the original values. | |||
| author | rwestMSFT | |||
| ms.author | randolphwest | |||
| ms.date | 05/19/2025 | |||
| ms.service | sql | |||
| ms.subservice | data-movement | |||
| ms.topic | concept-article | |||
| helpviewer_keywords |
|
|||
| monikerRange | >=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current |
[!INCLUDESQL Server Azure SQL Database Synapse Analytics PDW]
Data files that contain identity values can be bulk imported into an instance of Microsoft SQL Server.
By default, the values for the identity column in the data file that is imported are ignored and [!INCLUDEssNoVersion] assigns unique values automatically. The unique values are based on the seed and increment values that are specified during table creation.
If the data file does not contain values for the identifier column in the table, use a format file to specify that the identifier column in the table should be skipped when importing data. See Use a Format File to Skip a Table Column (SQL Server) for additional information.
To prevent [!INCLUDEssNoVersion] from assigning identity values while bulk importing data rows into a table, use the appropriate keep-identity command qualifier. When you specify a keep-identity qualifier, [!INCLUDEssNoVersion] uses the identity values in the data file.
These qualifiers are as follows:
| Command | Keep-identity qualifier | Qualifier type |
|---|---|---|
bcp |
-E |
Switch |
BULK INSERT |
KEEPIDENTITY |
Argument |
INSERT ... SELECT * FROM OPENROWSET(BULK...) |
KEEPIDENTITY | Table hint |
For more information, see bcp Utility, BULK INSERT (Transact-SQL), OPENROWSET BULK (Transact-SQL), INSERT (Transact-SQL), SELECT (Transact-SQL), and Table hints (Transact-SQL).
Note
To create an automatically incrementing number that can be used in multiple tables or that can be called from applications without referencing any table, see Sequence Numbers.
The examples in this topic are based on the table, data file, and format file defined below.
The script below creates a test database and a table named myIdentity. Execute the following Transact-SQL in Microsoft [!INCLUDEssManStudioFull] (SSMS):
CREATE DATABASE TestDatabase;
GO
USE TestDatabase;
CREATE TABLE dbo.myIdentity (
PersonID smallint IDENTITY(1,1) NOT NULL,
FirstName varchar(25) NOT NULL,
LastName varchar(30) NOT NULL,
BirthDate date
);Using Notepad, create an empty file D:\BCP\myIdentity.bcp and insert the data below.
3,Anthony,Grosse,1980-02-23
2,Alica,Fatnowna,1963-11-14
1,Stella,Rosenhain,1992-03-02
4,Miller,Dylan,1954-01-05
Alternatively, you can execute the following PowerShell script to create and populate the data file:
cls
# revise directory as desired
$dir = 'D:\BCP\';
$bcpFile = $dir + 'myIdentity.bcp';
# Confirm directory exists
IF ((Test-Path -Path $dir) -eq 0)
{
Write-Host "The path $dir does not exist; please create or modify the directory.";
RETURN;
};
# clear content, will error if file does not exist, can be ignored
Clear-Content -Path $bcpFile -ErrorAction SilentlyContinue;
# Add data
Add-Content -Path $bcpFile -Value '3,Anthony,Grosse,1980-02-23';
Add-Content -Path $bcpFile -Value '2,Alica,Fatnowna,1963-11-14';
Add-Content -Path $bcpFile -Value '1,Stella,Rosenhain,1992-03-02';
Add-Content -Path $bcpFile -Value '4,Miller,Dylan,1954-01-05';
#Review content
Get-Content -Path $bcpFile;
Invoke-Item $bcpFile;SQL Server support two types of format file: non-XML format and XML format. The non-XML format is the original format that is supported by earlier versions of SQL Server. For more information, see Use Non-XML format files (SQL Server).
The following command will use the bcp utility to generate a non-xml format file, myIdentity.fmt, based on the schema of myIdentity.
- To use a bcp command to create a format file, specify the
formatargument and usenulinstead of a data-file path. - The format option also requires the
-foption. cis used to specify character datat,is used to specify a comma as a field terminatorTis used to specify a trusted connection using integrated security.
At a command prompt, enter the following command:
bcp TestDatabase.dbo.myIdentity format nul -c -f D:\BCP\myIdentity.fmt -t, -T
REM Review file
Notepad D:\BCP\myIdentity.fmtImportant
Ensure your non-XML format file ends with a carriage return\line feed. Otherwise you will likely receive the following error message:
SQLState = S1000, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]I/O error while reading BCP format file
The examples use the database, datafile, and format files created in this article.
The -E switch.
At a command prompt, enter the following command:
REM Truncate table (for testing)
SQLCMD -Q "TRUNCATE TABLE TestDatabase.dbo.myIdentity;"
REM Import data
bcp TestDatabase.dbo.myIdentity IN D:\BCP\myIdentity.bcp -T -c -t, -E
REM Review results
SQLCMD -Q "SELECT * FROM TestDatabase.dbo.myIdentity;"The -E and -f switches.
At a command prompt, enter the following command:
REM Truncate table (for testing)
SQLCMD -Q "TRUNCATE TABLE TestDatabase.dbo.myIdentity;"
REM Import data
bcp TestDatabase.dbo.myIdentity IN D:\BCP\myIdentity.bcp -f D:\BCP\myIdentity.fmt -T -E
REM Review results
SQLCMD -Q "SELECT * FROM TestDatabase.dbo.myIdentity;"Using defaults.
At a command prompt, enter the following command:
REM Truncate table (for testing)
SQLCMD -Q "TRUNCATE TABLE TestDatabase.dbo.myIdentity;"
REM Import data
bcp TestDatabase.dbo.myIdentity IN D:\BCP\myIdentity.bcp -T -c -t,
REM Review results
SQLCMD -Q "SELECT * FROM TestDatabase.dbo.myIdentity;"Use defaults and -f switch.
At a command prompt, enter the following command:
REM Truncate table (for testing)
SQLCMD -Q "TRUNCATE TABLE TestDatabase.dbo.myIdentity;"
REM Import data
bcp TestDatabase.dbo.myIdentity IN D:\BCP\myIdentity.bcp -f D:\BCP\myIdentity.fmt -T
REM Review results
SQLCMD -Q "SELECT * FROM TestDatabase.dbo.myIdentity;"The KEEPIDENTITY argument.
Execute the following Transact-SQL in Microsoft [!INCLUDEssManStudioFull] (SSMS):
USE TestDatabase;
GO
TRUNCATE TABLE dbo.myIdentity; -- for testing
BULK INSERT dbo.myIdentity
FROM 'D:\BCP\myIdentity.bcp'
WITH (
DATAFILETYPE = 'char',
FIELDTERMINATOR = ',',
KEEPIDENTITY
);
-- review results
SELECT * FROM TestDatabase.dbo.myIdentity;The KEEPIDENTITY and the FORMATFILE arguments.
Execute the following Transact-SQL in Microsoft [!INCLUDEssManStudioFull] (SSMS):
USE TestDatabase;
GO
TRUNCATE TABLE dbo.myIdentity; -- for testing
BULK INSERT dbo.myIdentity
FROM 'D:\BCP\myIdentity.bcp'
WITH (
FORMATFILE = 'D:\BCP\myIdentity.fmt',
KEEPIDENTITY
);
-- review results
SELECT * FROM TestDatabase.dbo.myIdentity;Using defaults.
Execute the following Transact-SQL in Microsoft [!INCLUDEssManStudioFull] (SSMS):
USE TestDatabase;
GO
TRUNCATE TABLE dbo.myIdentity; -- for testing
BULK INSERT dbo.myIdentity
FROM 'D:\BCP\myIdentity.bcp'
WITH (
DATAFILETYPE = 'char',
FIELDTERMINATOR = ','
);
-- review results
SELECT * FROM TestDatabase.dbo.myIdentity;Using defaults and FORMATFILE argument.
Execute the following Transact-SQL in Microsoft [!INCLUDEssManStudioFull] (SSMS):
USE TestDatabase;
GO
TRUNCATE TABLE dbo.myIdentity; -- for testing
BULK INSERT dbo.myIdentity
FROM 'D:\BCP\myIdentity.bcp'
WITH (
FORMATFILE = 'D:\BCP\myIdentity.fmt'
);
-- review results
SELECT * FROM TestDatabase.dbo.myIdentity;The KEEPIDENTITY table hint and FORMATFILE argument.
Execute the following Transact-SQL in Microsoft [!INCLUDEssManStudioFull] (SSMS):
USE TestDatabase;
GO
TRUNCATE TABLE dbo.myIdentity; -- for testing
INSERT INTO dbo.myIdentity
WITH (KEEPIDENTITY)
(PersonID, FirstName, LastName, BirthDate)
SELECT *
FROM OPENROWSET (
BULK 'D:\BCP\myIdentity.bcp',
FORMATFILE = 'D:\BCP\myIdentity.fmt'
) AS t1;
-- review results
SELECT * FROM TestDatabase.dbo.myIdentity;Using defaults and the FORMATFILE argument.
Execute the following Transact-SQL in Microsoft [!INCLUDEssManStudioFull] (SSMS):
USE TestDatabase;
GO
TRUNCATE TABLE dbo.myIdentity; -- for testing
INSERT INTO dbo.myIdentity
(FirstName, LastName, BirthDate)
SELECT FirstName, LastName, BirthDate
FROM OPENROWSET (
BULK 'D:\BCP\myIdentity.bcp',
FORMATFILE = 'D:\BCP\myIdentity.fmt'
) AS t1;
-- review results
SELECT * FROM TestDatabase.dbo.myIdentity;To use a format file
To use data formats for bulk import or bulk export
-
Import native and character format data from earlier versions of SQL Server
-
Use unicode character format to import or export data (SQL Server)
-
Use Unicode Native Format to Import or Export Data (SQL Server)
To specify data formats for compatibility when using bcp