Methods for exporting data to XML and JSON in SQL Server _mssql

Source: Internet
Author: User
Tags ssis

Sometimes it may be useful to associate or analyze data from SQL Server to other departments at once, a requirement that is certainly simple for SSIS, but a lot of times it makes a fuss to create a SSIS package just by exporting the data one at a time, while the SQL The Import Export tool for the server there are a lot of bugs, the easiest way is to bcp.

Data exported to XML

After SQL Server 2005, a FOR XML clause is provided to natively support XML in a relational database. This command enables you to convert a two-dimensional relational result set to XML, which can be saved as XML by using bcp.

For example, the following data:

We can export this to an XML file by using the following bcp command (note that you cannot have a carriage return) and Save:

BCP select top [Bom_no],[level] from [Sqladmin]. [dbo]. [BOM] For XML Path,type, ELEMENTS, ROOT (' Regionsales ') "Queryout" d:\temp\test. XML "-c-t-t-s localhost

View the Test.xml file after the execution is complete, as shown in the following figure. You can see that the file format is very clear and can easily be imported into other systems.

Data exported as JSON

If you want to export data from SQL Server to JSON, although this operation already has a very mature approach in your application, SQL Server does not natively support this approach (gossip, the next version will support). I recommend the way to use this post: Http://jaminquimby.com/servers/95-sql/sql-2008/145-code-tsql-convert-query-to-json. After you have established the stored procedures provided by this post, use the following bcp command:

Upon completion of the execution, the results are shown below:

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.