Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi guys,

in my sql table i have one column of xml datatype.

In this col i have an xml without parent node, i mean, i have only one single node in this xml.

<FieldTicket FID="3113" TC="1" JobName="Edited:" PriceRef="2245" OurRef="2245" Qty="4.00" Price="118.00" SubTotal="0.00" Discount="0" />


how can i read this xml n fetch data from it, in SQL.

please help me.

Thanks

What I have tried:

if have parent node:
SELECT CustomerID, CustomerName, Address
FROM OPENXML(@hDoc, 'ROOT/Customers/Customer')
WITH 
(
CustomerID [varchar](50) '@CustomerID',
CustomerName [varchar](100) '@CustomerName',
Address [varchar](100) 'Address'
)
Posted
Updated 31-Mar-16 2:10am

1 solution

Try something like this:
SQL
SELECT
    X.Ticket.value('@FID', 'int') As FID,
    X.Ticket.value('@TC', 'int') As TC,
    X.Ticket.value('@JobName', 'varchar(50)') As JobName,
    X.Ticket.value('@PriceRef', 'int') As PriceRef,
    X.Ticket.value('@OurRef', 'int') As OurRef,
    X.Ticket.value('@Qty', 'float') As Qty,
    X.Ticket.value('@Price', 'money') As Price,
    X.Ticket.value('@SubTotal', 'money') As SubTotal,
    X.Ticket.value('@Discount', 'numeric') As Discount
FROM
    YourTable As T
    CROSS APPLY T.YourXmlColumn.nodes('//FieldTicket') As X (Ticket)
;
 
Share this answer
 
Comments
abdul subhan mohammed 6-Apr-16 2:09am    
Thanks dude

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900