SQL Server exports database records as XML and Json

Source: Internet
Author: User
Tags json

Export data to XML

After SQL Server 2005, a for xml clause is provided to support XML in relational databases. You can use this command to convert the result set of a two-dimensional link to XML, and use BCP to save the data as XML.

For example, the following data:


You can use the following BCP command (note that you cannot press enter) to export it as an XML file and save it:

BCP "select top 30 [bom_no], [LEVEL] FROM [sqladmin]. [dbo]. [bom] for xml path, TYPE, ELEMENTS, ROOT ('regionsales') "QUERYOUT" d: \ temp \ test. XML "-c-t-T-S localhost


After the execution is complete, view the Test. XML file, as shown in the following figure. You can see that the file format is very clear and can be easily imported to other systems.


Export data as JSON

If you want to export data in SQL Server as Json, although this operation has been very mature in the application, SQL Server does not actually support this method (gossip, supported in the next version ). I recommend using this post: Success. Run the following BCP command after the stored procedure provided in this post is created:


The result is as follows:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.