Java calls BCP to import data to the database to solve the problem of ID of the ID column

Source: Internet
Author: User

① 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.