I'm using bcp
to import a .csv
file into a table in SQL Server like this:
bcp test1 in "./file.csv" -S server_name -U login_id -P password -d database_name -c -t
I get this message/warning:
Unexpected EOF encountered in BCP data-file.
bcp copy in failed
file.csv
data:
A, B, C
A, B, C
A, B, C
A, B, C
My tables:
CREATE TABLE test2
(
a VARCHAR(8) PRIMARY KEY,
b VARCHAR(8),
c VARCHAR(8)
);
CREATE TABLE test1
(
ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
a VARCHAR(8),
b VARCHAR(8),
c VARCHAR(8)
FOREIGN KEY (a) REFERENCES test2(a)
);
Here is what I get in my SELECT * FROM test1;
:
ID | a | b | c |
1 B CA B C
A B CA B C
Here is what I expected:
ID|a |b |c
1 A B C
2 A B C
3 A B C
3 A B C
I'm having no issues with test2
which is nearly like test
but without ID
. So the csv
files are well formatted. So why do I have a shift like this?
EDIT 1
If I add header to csv
fileI have this in my SELECT * FROM test1
:
ID|a|b|c |
1 b cA B,C,A,B,C,A,B,C,A,B,C
EDIT 2
I generated a File Format
to guide my data.
13.0
3
1 SQLCHQR 0 40 "\t" 1 a SQL_Latin1_General_CP1_CI_AS
2 SQLCHQR 0 40 "\t" 2 a SQL_Latin1_General_CP1_CI_AS
3 SQLCHQR 0 40 "\t" 3 a SQL_Latin1_General_CP1_CI_AS
The modified one to try to "jump" over ID
13.0
4
1 SQLCHQR 0 40 "\t" 2 a SQL_Latin1_General_CP1_CI_AS
2 SQLCHQR 0 40 "\t" 3 a SQL_Latin1_General_CP1_CI_AS
3 SQLCHQR 0 40 "\t" 4 a SQL_Latin1_General_CP1_CI_AS
But I can't manage to make it work.
bcp test1 in "./file.csv" -S server_name -U login_id -P password -d database_name -f file_name -t
SQLState = S1002, NativeError = 0 Error = [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index
EDIT 3
I found a way around the problem, but it's still not the good solution. What I did is:
Change my test1
table, putting ID
column at the end.
Then adding with sed
double commas at the end of each lines in my CSV to create a new empty column. Then I made a simple bcp
in
. I still want keep ID
in first column, I just don't want to create an extra View
for putting ID
in front.