12

After reading one answer and second answer and the infopage on sqlcmd I still cannot get the following to work.

I am trying to query a result into a xml file using a sqlcmd in batch file.

The batchfile looks like this:

sqlcmd -R -d DBName -i "c:\inputquery.sql" -h-1 -y 0 -o "c:\outputfile.xml"

The simplicated sql query is:

:XML ON
SELECT '<?xml version="1.0" encoding="UTF-8"?>' +
CAST((
SELECT Columns FROM Table
FOR XML PATH ('Product'), ROOT('Products')
) 
AS NVARCHAR(MAX))

The output is a xml file of about 1025Kb whit a truncated string. I think it truncates to 1mb, but how can you prevent this? The goal is to get the full query result into the xml file. As far as I know, all options are used already. Using TSQL SSMS2008 by the way.

Community
  • 1
  • 1
J3FFK
  • 664
  • 3
  • 14
  • 32
  • If you're just working in SSMS, then there are options to control how much XML data can be returned. 1MB, 2MB, 5MB or Unlimited. See Tools | Options | Query Results | SQL Server | Results to Grid | XML data. It doesn't have an option for "Results to File". But perhaps you could run the query to the Grid, and then save the results? – James L. Jun 22 '14 at 04:44
  • 1
    If you use SSMS only then that would work, but I need to automate the process with a batch file. The SSMS settings don't have effect on the sqlcmd outcome. Thanks anyway! – J3FFK Jun 22 '14 at 14:52
  • 1
    I've used BCP to do things like this. Here's a BCP example: http://stackoverflow.com/a/2586615/822072 – James L. Jun 23 '14 at 14:48
  • Yes that's also a possibilty, I tried BCP, didn't work, but just found out xp_cmdshell is not active, see what it does if I fix that. I will try with SSIS package also. http://www.excel-sql-server.com/sql-server-export-to-excel-using-bcp-sqlcmd-xml.htm#SQL%20Server%20Data%20Export%20to%20XML%20using%20SQLCMD – J3FFK Jun 23 '14 at 15:11
  • After exhausting all visible options to **sqlcmd.exe** and SSMS, I wrote a Java program to extract the XML column I needed. Hardly a universal solution, but it got this job done. – gws Jan 11 '17 at 19:18

3 Answers3

19

I ran into the same issue today, I used the -y0 option. My output looks correct now. Thought I would post my findings so it might help others running into the same thing.

sqlcmd -Sxxxxxxx -E -dmaster -h-1 -y0 -Q" my query that produces long results in here;" > "D\:out.txt"

-h-1 removes the column headers too.

ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
Cameron Francey
  • 206
  • 2
  • 3
  • `-y0`'s maximum is one megabyte. (I've got the same problem as OP where 1048676 characters are not enough.) – gws Jan 11 '17 at 15:59
  • 4
    When I tried using `-h-1` and `-y0` I got this error message: `Sqlcmd: The -h and the -y 0 options are mutually exclusive.`. Although it seems like even without `-h-1` the header is excluded anyways. – ArtOfWarfare Jan 09 '18 at 16:42
1

Using -y0 by itself opened up the widest column so far to 664,241. I don't know what the limit is, but this seems quite good. The -h-1 is for no headers.

sqlcmd -S XXXXX -E -o test.txt -i SelectStmt.spl -h-1 -y0

The SelectStmt.spl has 40 fields and one field is a binary picture which is why one column width is so wide.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
-1

You need to output your XML as XML type. Normally, that is done using TYPE directive in addition to FOR XML. But if you want to add the Xml declaration, you will have to put everything into an xml variable then select that. Ex:

declare @xml as xml
select @xml = cast(('<?xml version="1.0" encoding="UTF-16"?>'+cast((SELECT Columns FROM Table FOR XML PATH ('Product'), ROOT('Products')) as nvarchar(max))) as xml)

select @xml

Edit: Sorry. Adding Xml declaration will not work because Sql presents the stream as XML in its native encoding (UTF-16) and it will strip off any declaration.

Edit:

alans
  • 1,022
  • 9
  • 17