# XML format files in BCP example needed ...

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
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
HI

http://www.mssqltips.com/tip.asp?tip=1060
Commented:
SQL Server Books Online has good examples
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

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...

Experts Exchange Solution brought to you by