4

So i'm having a bit of trouble to figure out how i could make this work so that i can append the xml encoding to my query.

this is what i hacked together so far:

DECLARE @FileName VARCHAR(50)
DECLARE @SQLCmd   VARCHAR(8000)

SELECT  @FileName = 'C:\SampleXMLOutput.xml'

-- in this command, we are making sure there is only one ROOT node


SELECT @SQLCmd = + 'bcp ' + 
                         '"SELECT Id, Initials, firstname, lastname, email ' +
                         ' FROM Employees.dbo.IDCards ' +
                         ' FOR XML PATH(''Employee''), ELEMENTS,  ROOT(''Employees''), TYPE "' +
                         ' queryout '  +
                   @FileName +
                   ' -w -T -S' + @@SERVERNAME



-- display command, for visual  check
SELECT @SQLCmd AS 'Command to execute'

-- create the XML file
EXECUTE master..xp_cmdshell @SQLCmd

Also i need to get some data from another table within the same query output.

Any help appreciated

JJonson
  • 57
  • 1
  • 7
  • Possible duplicate of [SQL Server FOR XML PATH: Set processing instruction "xml-stylesheet" on top](http://stackoverflow.com/questions/33806454/sql-server-for-xml-path-set-processing-instruction-xml-stylesheet-on-top) – Shnugo May 25 '16 at 14:12
  • I doubt this is correctly tagged. Does MySQL support `FOR XML PATH` ???. The given syntax and the `bcp` points to SQL Server... – Shnugo May 25 '16 at 14:29
  • it's for SQL server – JJonson May 25 '16 at 15:16

1 Answers1

2

I assume - due to the syntax - that you are using SQL Server...

With FOR XML PATH it is possible to create a processing instruction like this

SELECT 'test' AS OneNormalElement
      ,'version="1.0" encoding="UTF-8"' AS [processing-instruction(abc)]
FOR XML PATH('Test')

The result

<Test>
  <OneNormalElement>test</OneNormalElement>
  <?abc version="1.0" encoding="UTF-8"?>
</Test>

But you are not allowed to use AS [processing-instruction(xml)]. You get the error:

Error: 6879, Severity: 16, ‘xml’ is an invalid XML processing instruction target. Possible attempt to construct XML declaration using XML processing instruction constructor. XML declaration construction with FOR XML is not supported.

It is actually not supported to create any PI outside of your XML...

The question I've linked as "duplicate" shows some workarounds how to add this anyhow...

UPDATE

I must apologize for the late response as well as for the wrong hint. The xml-declaration is handled differently, so the linked answer did not help actually. I updated this also...

The only way I found to add an XML-declaration is string concatenation:

DECLARE @ExistingXML XML=
(
    SELECT 
        'Test' AS Test,
        'SomeMore' AS SomeMore
    FOR XML PATH('TestPath'),TYPE
);

DECLARE @XmlWithDeclaration NVARCHAR(MAX)=
(
    SELECT '<?xml version="1.0" encoding="UTF-8"?>'
           +
           CAST(@ExistingXml AS NVARCHAR(MAX))
);
SELECT @XmlWithDeclaration;

And if you want to do it in one single call, you must do this without ,TPYE

DECLARE @XmlWithDeclaration NVARCHAR(MAX)=
(
    SELECT '<?xml version="1.0" encoding="UTF-8"?>'
           +
            (
            SELECT 
                'Test' AS Test,
                'SomeMore' AS SomeMore
            FOR XML PATH('TestPath')
            )
);
SELECT @XmlWithDeclaration;

So - finally - I hope this is your solution:

Edit: doubled quotes. See comments.

SELECT @SQLCmd = + 'bcp ' + 
                     '"SELECT ''<?xml version=""1.0"" encoding=""UTF-8""?>'' + ' + 
                     ' (SELECT Id, Initials, firstname, lastname, email ' +
                     '  FROM Employees.dbo.IDCards ' +
                     '  FOR XML PATH(''Employee''), ELEMENTS,  ROOT(''Employees'')) "' +
                     ' queryout '  +
               @FileName +
               ' -w -T -S' + @@SERVERNAME
Shnugo
  • 66,100
  • 9
  • 53
  • 114
  • Wouldn't your example add the encodig to every element ? I only want it appended in the beginning. How would it look in the code i pasted? – JJonson May 25 '16 at 15:18
  • @user6381438 did you read the link I posted below your question as possible duplicate? – Shnugo May 25 '16 at 15:31
  • Yes i looked at the link but i'm not sure how to go about it with the code i already put together? – JJonson May 25 '16 at 15:53
  • First of all, no need to apologize for the late response and the hint. I really appreciate the time you've spent just trying to be helpful :) @Shnugo Thanks Shnugo ! that seemed to do the trick.. almost :) The hyphens are not carried over in the output. so instead of output is – JJonson May 30 '16 at 12:17
  • Hi @user6381438, according to [this](http://www.sqlservercentral.com/Forums/Topic1500518-392-1.aspx) BCP wants the inner double quotes doubled: `` – Shnugo May 30 '16 at 12:30
  • Hmm i'm pretty sure i tried that and they were not there. Tried again and they sure indeed are where they should be ;) Again thanks a bunch for your time!! – JJonson May 30 '16 at 14:06
  • I've been trying to SELECT from multiple tables, but i can only get it to work with CROSS JOIN. – JJonson Jun 13 '16 at 08:25
  • Sorry i tried to edit the comment, but i was to late, and the rest of the code i thought i had pasted didn't show. I've Created a new question [link](https://stackoverflow.com/questions/37785553/select-from-multiple-tables-without-iteration) - Hoped you would have the time to have a look at it since you helped with the other issue :) – JJonson Jun 13 '16 at 09:21
  • Had to create a new question [link](https://stackoverflow.com/questions/37785553/select-from-multiple-tables-without-iteration) – JJonson Jun 13 '16 at 10:51