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

前端 未结 2 1522
攒了一身酷
攒了一身酷 2020-12-22 00:22

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

Since the bcp file is generated by another xml pa

相关标签:
2条回答
  • 2020-12-22 00:54

    Sybase format files can be tricky to use, and I have yet to get them to work to redorder columns. Without a format file, and since you can not control the input file you're given, your options are:

    1. Rebuild the table with the columns in the correct order
    2. Use a temp table, or work table for the bcp in, and move the data into your production table from there.
    3. If for some reason there is logic in place the requires the table to keep it's existing column order (which should be unlikely), you can rebuild the table with the correct column order, then create a view of the table that has the columns in the old order.

    Given your data format, the following bcp command line should work.

    bcp DBNAME..TABLENAME in MY_FILE.bcp -U username -P password -S SERVERNAME -c -t\| -r\\n
    
    0 讨论(0)
  • 2020-12-22 01:07

    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\n603284|Aug 22 2011 12:00AM|XXX bbb OVD|KLPK06
    

    or this:

    603289|Aug 20 2011 12:00AM|YYY aaa OVD|KLYYP8\n\r603284|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   "\n" 1 name2   * The last terminator was not "|"
    

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

    • By the way, please notice the last terminator!
    0 讨论(0)
提交回复
热议问题