Extracting large XML data using bcp
Posted by decipherinfosys on October 4, 2009
Ran into an issue today at the client site. There was a stored procedure in the outbound interface of the client application which was responsible for collecting event based data, convert it into an XML and then the SSIS package would take that XML document, encrypt it using pgp encryption and upload it to the client’s partner companies. Everything was working like a charm for the past couple of months till last night, out of memory exceptions started coming.
The reason was that the XML that was generated was close to 200MB that night – this was because of a large amount of event activity in the system which resulted into a generation of a very large XML. When the SSIS package was trying to get that data out to encrypt it, it started running into the out of memory exceptions. The fix that we made was pretty straight forward. We used bcp to extract out the large XML document. Here are the steps:
1) First, generate the format file. A very simple way of doing this is to let the bcp command do it for you. Here is a sample example:
bcp deciphertest.dbo.large_xml_tbl format nul -T -c -x -f “D:\format_files\large_xml_tbl.xml”
This command takes the table large_xml_tbl from the deciphertest database andd dumps the format file “large_xml_tbl.xml” on the D:\format_files folder. This is just to illustrate the usage. In real life, one would be using a query to extract the XML document out rather than the table itself.
2) Once the format file has been generated, you can then use the bcp command to extract out the large XML:
bcp deciphertest.dbo.large_xml_tbl out “D:\XML_Data\large_xml_tbl_1004090815.xml” -SDISprod -T -f “D:\format_files\Test.xml”
This took about 4-5 seconds for the 200MB file. The encryption was done then and file was uploaded using the regular process.