When using SqlBulkCopy, you should be aware of columns that use default values in SQL Server tables _mssql

Source: Internet
Author: User
SqlBulkCopy a given value of type String from a data source cannot be converted to the type nvarchar of the specified destination column.



Found on the Internet, mostly because the field in the database is too small (

A given value of type String from a data source cannot be converted to the type nvarchar of the specified destination column.
), resulting in truncation errors, after careful examination, I found that there is no field in my table design is too small, nor is it a single quotation mark problem.

After careful debugging, I found that my text file does not correspond to the table columns in the library due to the use of sqlbulkcopy import. There are 18 fields in my text file, and I have only 9 fields in my table, two of which are not in the text file (that is, the level and [Cagegory] fields in table Structure 1), and the default values are filled in by the database. The table structure is as follows:

Copy Code code as follows:

Table Structure 11
CREATE TABLE [dbo]. [Ryxx] (
[Name] [nvarchar] () not NULL,
[Idcardno] [nvarchar] () not NULL,
[Sex] [nvarchar] (2) Not NULL,
[Height] [nvarchar] (5) NULL,
[Level] [nvarchar] (2) NULL,--default is a
[Category] [nvarchar] () NULL,--default to "Focus management"
[Sponsor] [nvarchar] (MB) NULL,
[Contact] [nvarchar] (a) NULL,
[Phone] [nvarchar] (m) NULL,
[NUMBER] [nvarchar] () NULL

) on [PRIMARY]

Table Structure 1

The modified table structure is as follows
Copy Code code as follows:

CREATE TABLE [dbo]. [Ryxx] (
[Name] [nvarchar] () not NULL,
[Idcardno] [nvarchar] () not NULL,
[Sex] [nvarchar] (2) Not NULL,
[Height] [nvarchar] (5) NULL,
[Sponsor] [nvarchar] (MB) NULL,
[Contact] [nvarchar] (a) NULL,
[Phone] [nvarchar] (m) NULL,
[NUMBER] [nvarchar] (a) NULL,
[Level] [nvarchar] (2) NULL,--default is a
[Category] [nvarchar] () NULL--Default to "Focus management"
) on [PRIMARY]

So far, the problem is solved. The reason for this is because the fields obtained from the text file correspond to the default columns in the table, resulting in truncation, which should also occur if you want to use BCP.

Related Article

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.