BCP import using a format file

Blake Duffey 41 Reputation points
2023-07-25T18:06:12.52+00:00

I have a simple test database - 4 columns, each nchar(20).

I generate a BCP format file via: bcp dbo.Table_2 format nul -x -n -f "C:\Table_2_format.xml" -d test -S sqldev -T

When I run: bcp dbo.Table_2 IN "C:\input2.csv" -f "C:\Table_2_format.xml" -d test -S sqldev -T

I get: Error = [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]A required attribute ID is missing in xml format file for the field at line 5 column 102.

So I generate a non-XML format file via: bcp dbo.Table_2 format nul -n -f "C:\Table_2_format.fmt" -d test -S sqldev -T

When I run the bcp import using this file, I get:

Starting copy...

0 rows copied.

Network packet size (bytes): 4096

Clock Time (ms.) Total : 47

Since the format file is being generated by bcp, I'm confused why the XML would not be valid. I'm then confused why the non-XML file wouldn't import.

SQL Server Other
{count} votes

Accepted answer
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2023-07-25T18:58:15.7266667+00:00

    I did this on SQL 2019:

    CREATE TABLE bcptest (col1 nchar(20) NULL,
                          col2 nchar(20) NULL,
                          col3 nchar(20) NULL,
                          col4 nchar(20) NULL)
    INSERT bcptest(col1, col2, col3, col4)
       VALUES('Stravinsky', 'Chaikovsky', 'Rimsy-Korsakov', 'Prokofieff'),
             ('Debussy', 'Ravel', 'Saint-Saëns', 'Chopin')
    
    
    

    Then I ran in a command-line window:

    bcp tempdb.dbo.bcptest format nul -x -f slask.fmt -n -S .\DIXNEUF -T
    bcp tempdb.dbo.bcptest format out -f slask.fmt  -S .\DIXNEUF -T
    

    This completed normally. Then I ran in SQL:

    TRUNCATE TABLE bcptest
    

    And back in the command-line window I ran:

    bcp tempdb.dbo.bcptest in slask.bcp -f slask.fmt  -S .\DIXNEUF -T
    

    This completed successfully.

    Now there are some details missing from your post:

    1. The actual CREATE TABLE statement.
    2. Which version of bcp you are using. (bcp -v will tell you.)
    3. The actual format file generated.

    There is also one thing that strikes me as funny. The file has a suffix of .csv, but you use the option -n for native format. CSV is text format, but native format is binary. In a native file there are no delimiters. Integer values are always four bytes. And nchar(20) values are prefixed by two bytes giving the length of the values.

    Also, you would use the option to generate a format file, if you are exporting a table to import it elsewhere. If you have a file you want to import, you will have to craft the format file by hand (if format cannot be expressed with help of the -t and -r options.)

    Of the two formats for the format file, I much prefer the old one. The XML format does not give any extra powers, but is only bulky.


2 additional answers

Sort by: Most helpful
  1. Konstantinos Passadis 19,586 Reputation points MVP
    2023-07-25T18:22:19.8+00:00

    Removed ,

    Apologies


  2. Vahid Ghafarpour 23,385 Reputation points Volunteer Moderator
    2023-07-25T18:22:59.8733333+00:00

    First of all, make sure that the path is correct "C:\intel\Table_2_format.xml" and "C:\Table_2_format.xml"

    And next, ensure that the XML file is well-formed and contains all the required attributes for each field. One missing or incorrectly formatted attribute can lead to this error.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.