Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
711 views
in Technique[技术] by (71.8m points)

sybase - how to specify row delimiter when using bcp with a format file

I am using bcp utility to import data into Sybase database on Solaris system

Since the bcp file is generated by another xml parsing script, I have no control on that to make it generate the fields in the order I want. And now the the order of fields in the bcp file is a little different with the order in the database table.

I want to use a format file for bcp tool to control the order of fields loaded into the database, so I have the sample bcp file as below and constructed a format file accordingly:

the bcp file:

603289|Aug 20 2011 12:00AM|YYY aaa OVD|KLYYP8
603284|Aug 22 2011 12:00AM|XXX bbb OVD|KLPK06

the format file:

10.0
4
1  SYBCHAR   0  12   "|" 3 ver
2  SYBCHAR   0  26   "|" 2 first_dt
3  SYBCHAR   0  60   "|" 4 name1
4  SYBCHAR   0  10   "|" 1 name2

Although I am stuck on the following error:

$bcp my_db..my_tbl in test.bcp -e error -f format.fmt -r\n -S Sever -U user -P pw

Starting copy...
CSLIB Message:  - L0/O0/S0/N24/1/0:
cs_convert: cslib user api layer: common library error: The conversion/operation was stopped due to a syntax error in the source field.
Unexpected EOF encountered in BCP data-file.
bcp copy in partially failed

1 rows copied.

I suspect the cause for the error is the bcp utility can't recognize the row delimiter which is ' ' character (I have used od -c test.bcp to check this), although the -r\n option in the command seems doesn't work, which should specify the row delimiter as ' ' character.

Does anyone have a clue?

Edit:

I did a change on the format file and it works fine now, I changed the field delimiter for the last field from '|' to ' ' as below:

the new format file:

10.0
4
1  SYBCHAR   0  12   "|" 3 ver
2  SYBCHAR   0  26   "|" 2 first_dt
3  SYBCHAR   0  60   "|" 4 name1
4  SYBCHAR   0  10   "
" 1 name2

As Michael mentioned in comments, it's tricky to use format files. When using the original format file I have tried to add '|' at end of each records, although the file were not processed correctly.

Now the file can be processed correctly, although I am not sure what is used as row delimiter by bcp utility

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As most of the loader utilities, BCP does not read rows, it just gets a bunch of bytes from the source file, therefore, you have to specify the end line character on the .fmt file mostly when the end of line character can be different depending on the encoding of the file.

For example, in the example you provided, you have this on the source file:

603289|Aug 20 2011 12:00AM|YYY aaa OVD|KLYYP8
603284|Aug 22 2011 12:00AM|XXX bbb OVD|KLPK06

But depending on the Encoding you will have this:

603289|Aug 20 2011 12:00AM|YYY aaa OVD|KLYYP8
603284|Aug 22 2011 12:00AM|XXX bbb OVD|KLPK06

or this:

603289|Aug 20 2011 12:00AM|YYY aaa OVD|KLYYP8

603284|Aug 22 2011 12:00AM|XXX bbb OVD|KLPK06

That's why the change of the terminator you did on the last column is so important:

10.0
4
1  SYBCHAR   0  12   "|" 3 ver
2  SYBCHAR   0  26   "|" 2 first_dt
3  SYBCHAR   0  60   "|" 4 name1
4  SYBCHAR   0  10   "
" 1 name2   * The last terminator was not "|"

The following image shows what means each component on the format file (I got it from here):

enter image description here

  • By the way, please notice the last terminator!

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...