Solved

# XML format files in BCP example needed ...

Posted on 2008-11-15
1,140 Views
Hi,

Can you please provide me a link / article on XML format files in BCP ? I want to know how the XML format file will be used to either - to remove some columns while import / export or changing the positions of the columns.

Thanks
0
Question by:milani_lucie

LVL 19

Expert Comment

HI

http://www.mssqltips.com/tip.asp?tip=1060
0

LVL 18

Expert Comment

SQL Server Books Online has good examples
0

LVL 51

Expert Comment

The XML Format files really just describe the columns used in the import file... If you have played with format files, then it is not such a huge leap.

Basically in two sections... RECORDS describe the incoming record layout, ROWS describe the receiving table. Then is is pretty much a matter of manipulating what and how you want - like field and record terminators, lengths, skipping columns, data types etc. Now it does seem to be a bit more "fussy" than the old format, but it is more flexible.

Still recommend importing into a generic staging table then populating the real table from there... It also keeps the format file pretty easy...

The experts above have posted links, so I will just post a sample below...

incoming DATA file looks like :

1;markwills;ee

2;jmoss111;ee

3;elimesika;ee

populating a table that looks like :

create table my_import_ee_bestest (ee_id int, ee_signature varchar(20), ee_origin varchar(20))

then the appropriate FORMAT file would look like (and propably best to use real column names) :

<BCPFORMAT

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<RECORD>

<FIELD ID="1" xsi:type="CharTerm" TERMINATOR=";"    MAX_LENGTH="20" />

<FIELD ID="2" xsi:type="CharTerm" TERMINATOR=";"    MAX_LENGTH="20" />

<FIELD ID="3" xsi:type="CharTerm" TERMINATOR="\r\n" MAX_LENGTH="20" />

</RECORD>

<ROW>

<COLUMN SOURCE="1" NAME="TCOLUMN1" xsi:type="SQLINT"/>

<COLUMN SOURCE="2" NAME="TCOLUMN2" xsi:type="SQLVARYCHAR"/>

<COLUMN SOURCE="3" NAME="TCOLUMN3" xsi:type="SQLVARYCHAR"/>

</ROW>

</BCPFORMAT>

So, if you save the above DATA file as c:\EE\DATA.txt and the above format file as C:\EE\FORMAT.xml then the following command will work:

select * from openrowset(bulk 'c:\ee\data.txt', formatfile = 'c:\ee\format.xml') as a

0

LVL 51

Accepted Solution

Oh, and just to finish off....

The RECORD part must match the incoming file, no questions as far as I am concerned - it helps validate / checks the layout as well, but if you want to skip a few columns, then in the ROWS section, simply remove those lines : e.g. if we no longer need the third column :

<BCPFORMAT
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RECORD>
<FIELD ID="1" xsi:type="CharTerm" TERMINATOR=";"    MAX_LENGTH="20" />
<FIELD ID="2" xsi:type="CharTerm" TERMINATOR=";"    MAX_LENGTH="20" />
<FIELD ID="3" xsi:type="CharTerm" TERMINATOR="\r\n" MAX_LENGTH="20" />
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="TCOLUMN1" xsi:type="SQLINT"/>
<COLUMN SOURCE="2" NAME="TCOLUMN2" xsi:type="SQLVARYCHAR"/>
</ROW>
</BCPFORMAT>

or, if we wanted to swap column 2 for 3 then :

<BCPFORMAT
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RECORD>
<FIELD ID="1" xsi:type="CharTerm" TERMINATOR=";"    MAX_LENGTH="20" />
<FIELD ID="2" xsi:type="CharTerm" TERMINATOR=";"    MAX_LENGTH="20" />
<FIELD ID="3" xsi:type="CharTerm" TERMINATOR="\r\n" MAX_LENGTH="20" />
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="TCOLUMN1" xsi:type="SQLINT"/>
<COLUMN SOURCE="3" NAME="TCOLUMN2" xsi:type="SQLVARYCHAR"/>
<COLUMN SOURCE="2" NAME="TCOLUMN3" xsi:type="SQLVARYCHAR"/>
</ROW>
</BCPFORMAT>

So, it is a lot more flexible now...
0

## Featured Post

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

#### Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!