Export xml data using BCP Command in SQL Server

天涯浪子 提交于 2019-11-28 05:09:40

问题


I am trying to export datable data in xml format but,Problem is like i can able to create xml file but data is not get writing in to the file following is my code I am trying.

DECLARE @cmd  VARCHAR(2000);

SET @cmd = 'bcp.exe "select * from emp FOR XML AUTO" queryout E:\rahul_1.xml -x -T';

EXEC xp_cmdshell  @cmd ;

And following is the output message I am getting after executing above code

NULL
Enter the file storage type of field XML_F52E2B61-18A1-11d1-B105-00805F49916B [ntext]: 

can any body please suggest me on this


回答1:


Dan you answer works, except one last thing. BCP needs additional information about the source query. Best idea to fully qualify the source of the data.

SET @cmd = 'bcp.exe "select * from [Database].[Schema].[Table] FOR XML AUTO" 
             queryout E:\rahul_1.xml -c -T';



回答2:


try to use -w switch for exporting XML file in correct format

SET @cmd = 'bcp.exe "select * from [Database].[Schema].[Table] FOR XML AUTO" queryout E:\filename.xml -S MyServer\MyInstance -c -T -w';




回答3:


Instead of the -x parameter (generate xml format file), specify -c (character file):

SET @cmd = 'bcp.exe "select * from emp FOR XML AUTO" queryout E:\rahul_1.xml -c -T';


来源:https://stackoverflow.com/questions/27150672/export-xml-data-using-bcp-command-in-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!