① The data to be imported is as follows:
The red box indicates the ID column, which is automatically increased.
However, we use
Bcp sportSys. dbo. competitions in bytes 1competitions.xls-c-T> % 2import.txt
② After the data is imported, it is found that there is a problem with the data.
We can clearly find that the ID has changed, and the resulting problems can be imagined. How can we solve this problem?
Some people have proposed the following method:
SET IDENTITY_INSERT tb ON -- Insert the explicit value into the table's ID column.
Insert .....
SET IDENTITY_INSERT tb OFF -- disable the option after completion
When this statement is used, only one table can be imported, and the meaning of batch import is lost.
In addition, you will be prompted to directly write it in our bat file.
SET IDENTITY_INSERT sportSys. dbo. compet
Itions on
The environment variable IDENTITY_INSERT sportSys. dbo. competitions is not defined.
After reading the documentation, we found that bcp has provided us with a very good solution, coupled with-E
This parameter solves the problem of identifying columns!
The following is a detailed description of the-E parameter,
-E
Specifies that identity value or values in the imported data file are to be used for the identity column. if-E is not given, the identity values for this column in the data file being imported are ignored, and SQL Server automatically assigns unique values based on the seed and increment values specified during table creation.
If the data file does not contain values for the identity column in the table or view, use a format file to specify that the identity column in the table or view shocould be skipped when importing data; SQL Server automatically assigns unique values for the column. for more information, see dbcc checkident (Transact-SQL ).
The-E option has a special permissions requirement. For more information, see "Remarks" later in this topic.
If the-E parameter is not added during bcp import, SQL Server automatically processes the ID column in the target table, therefore, the ID value is not what we want.
Author: w1564000045