SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Server - FOR XML Clause


SQL Server - FOR XML Clause

Author
Message
caf 13920
caf 13920
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 16
Hello. I'm trying to generate an XML file from SQL according to a some guidelines, which works fine but one field I cannot generate it as it is requested or at least I don't know how:

Requested:
<InvoicedQuantity unitCode="BUC">37.00</InvoicedQuantity>

What I managed to generate so far, looks like this:
<InvoicedQuantity>37.00</InvoicedQuantity>

SQL Code:

SELECT
LineNumberID AS ID,
CONVERT(decimal(18, 2), InvoicedQuantity) AS InvoicedQuantity,
FROM (SELECT LineGID FROM MyTable WHERE GUID=@NR2
FOR XML PATH('TaxScheme'), ROOT('TaxCategory'), TYPE)


What am I trying to do is to add that unitCode after the opening of InoicedQuantity tag. Is this possible somehow? I tried to add as an attribute but it does not meet the requirements.

Thanks
Matt Miller (4)
Matt Miller (4)
SSC Guru
SSC Guru (103K reputation)SSC Guru (103K reputation)SSC Guru (103K reputation)SSC Guru (103K reputation)SSC Guru (103K reputation)SSC Guru (103K reputation)SSC Guru (103K reputation)SSC Guru (103K reputation)

Group: General Forum Members
Points: 103492 Visits: 20794
caf 13920 - Friday, February 23, 2018 7:36 AM
Hello. I'm trying to generate an XML file from SQL according to a some guidelines, which works fine but one field I cannot generate it as it is requested or at least I don't know how:

Requested:
<InvoicedQuantity unitCode="BUC">37.00</InvoicedQuantity>

What I managed to generate so far, looks like this:
<InvoicedQuantity>37.00</InvoicedQuantity>

SQL Code:

SELECT
LineNumberID AS ID,
CONVERT(decimal(18, 2), InvoicedQuantity) AS InvoicedQuantity,
FROM (SELECT LineGID FROM MyTable WHERE GUID=@NR2
FOR XML PATH('TaxScheme'), ROOT('TaxCategory'), TYPE)


What am I trying to do is to add that unitCode after the opening of InoicedQuantity tag. Is this possible somehow? I tried to add as an attribute but it does not meet the requirements.

Thanks
unitCode in your request is an XML attribute of the InvoicedQuantity element. XML attributes get expressed with a leading @ symbol in SQLXML.

Here's an example of how to add the literal value to your query.

SELECT
LineNumberID AS ID,'BUC' as "InvoicedQuantity/@unitCode",
CONVERT(decimal(18, 2), InvoicedQuantity) AS InvoicedQuantity,
FROM (SELECT LineGID FROM MyTable WHERE GUID=@NR2
FOR XML PATH('TaxScheme'), ROOT('TaxCategory'), TYPE)


----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
caf 13920
caf 13920
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 16
Matt Miller (4) - Friday, February 23, 2018 7:53 AM
caf 13920 - Friday, February 23, 2018 7:36 AM
Hello. I'm trying to generate an XML file from SQL according to a some guidelines, which works fine but one field I cannot generate it as it is requested or at least I don't know how:

Requested:
<InvoicedQuantity unitCode="BUC">37.00</InvoicedQuantity>

What I managed to generate so far, looks like this:
<InvoicedQuantity>37.00</InvoicedQuantity>

SQL Code:

SELECT
LineNumberID AS ID,
CONVERT(decimal(18, 2), InvoicedQuantity) AS InvoicedQuantity,
FROM (SELECT LineGID FROM MyTable WHERE GUID=@NR2
FOR XML PATH('TaxScheme'), ROOT('TaxCategory'), TYPE)


What am I trying to do is to add that unitCode after the opening of InoicedQuantity tag. Is this possible somehow? I tried to add as an attribute but it does not meet the requirements.

Thanks
unitCode in your request is an XML attribute of the InvoicedQuantity element. XML attributes get expressed with a leading @ symbol in SQLXML.

Here's an example of how to add the literal value to your query.

SELECT
LineNumberID AS ID,'BUC' as "InvoicedQuantity/@unitCode",
CONVERT(decimal(18, 2), InvoicedQuantity) AS InvoicedQuantity,
FROM (SELECT LineGID FROM MyTable WHERE GUID=@NR2
FOR XML PATH('TaxScheme'), ROOT('TaxCategory'), TYPE)

Thanks. This is exactly what I needed.

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search