SQL exports data to an XML file

Source: Internet
Author: User

There are two ways to export an XML file: A direct export to an XML file, no need to create a table to store XML text to the database, but if the value of the Chinese language can not open the XML file normally, to be studied, on the code: it is important to note that the query data when the table must be followed with the database. Table name

1 Create proc [dbo].[Outputxml] 

As
Declare @strsql VARCHAR(4000)

Declare @strPath VARCHAR( -)
Declare @str VARCHAR( $)
SELECT @str= Convert(varchar( A),getdate()-1, the)

SET @strPath='F:\XMLOutFile\'

SET @strPath+='P_'+@str

SET @strPath+='. XML'

SET @strsql='BCP "SELECT ID, isnull (name," '") as Rname from Tdb. Tabletest'

SET @strsql+='For XML PATH ("'Roots"'), TYPE, ELEMENTS, ROOT ("'ROOTP"') "Queryout"'

SET @strsql+=@strPath
SET @strsql+='"-c-t-t-s localhost'

EXECxp_cmdshell@strsql

Method Two: You need to create a table to store the text


INSERT INTO TDB. Xmltab values (' <?xml version= ' 1.0 "encoding=" GB2312 "?> ') declare @x XML set @x= (SELECT ID, isnull (name," ") as RNA Me from Tdb. Tabletest for XML PATH (' Roots '), TYPE, ELEMENTS, ROOT (' ROOTP ')) insert into Tdb. Xmltab Select CAST (@x as varchar (max)) SET @strsql = ' bcp Tdb: Dbo.xmltab out ' set @[email protected] Set @strsql + = '-c-t-K ' EXEC master. xp_cmdshell @strsql DELETE as_product. Xmltab--delete data

  

SQL exports data to an XML file

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.