6

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
  • On a side note, the documentation explicitly says The -x does not work when importing or exporting data. Commented Jan 19, 2016 at 8:50

3 Answers 3

9

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';
Sign up to request clarification or add additional context in comments.

Comments

1

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';

2 Comments

Thanks for replay I tried this but it is also not working,following message i got after execution command NULL Starting copy... NULL 0 rows copied. Network packet size (bytes): 4096 Clock Time (ms.) Total : 1 NULL
This command worked great for a very similar effort for exporting Extended Event outputs to an external file. +1
1

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

DECLARE @cmd VARCHAR(2000);

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

EXEC xp_cmdshell @cmd;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.