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

elimesikaCommented:
HI

Take a look at this article
http://www.mssqltips.com/tip.asp?tip=1060
0
jmoss111Commented:
SQL Server Books Online has good examples
0
Mark WillsTopic AdvisorCommented:
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="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" 
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

Open in new window

0
Mark WillsTopic AdvisorCommented:
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="http://schemas.microsoft.com/sqlserver/2004/bulkload/format"
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="http://schemas.microsoft.com/sqlserver/2004/bulkload/format"
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.