I've have a table which contains XML data with several nodes in one of the columns. Something similar to the below.
<SendWorkOrders xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
creationDateTime="2021-06-05T10:16:09.713+12:00"
requestCorrelationId="63cbec24c87a1bfa88999ef373e"
xmlns="http://mycompany.co.nz/customerservices/servicepartner/workorder">
<WORKORDERSET>
<WORKORDER action="Replace">
<WONUM>123456</WONUM>
<DESCRIPTION>My work order</DESCRIPTION>
</WORKORDER>
</WORKORDERSET>
</SendWorkOrders>
I want to change the format to something like below in order to use XQUERY and filter required nodes.
<SendWorkOrders>
<WORKORDERSET>
<WORKORDER>
<WONUM>123456</WONUM>
<DESCRIPTION>My work order</DESCRIPTION>
</WORKORDER>
</WORKORDERSET>
</SendWorkOrders>
Here's more details.
I have a table called 'WorkOrderLogs' which has 4 columns with EventId,Source,Timestamp,SendWorkOrders. SendWorkOrders column stores the raw XML from customer as mentioned in the first code snippet. I'm trying to reference specific nodes in the XML value using the query below.
SELECT e.EventId,e.Timestamp,e.SendWorkOrder,e.Source,
X.Y.value('(WONUM)[1]','VARCHAR(20)') as WONUMBER,
X.Y.value('(DESCRIPTION)[1]','VARCHAR(256)') as DESCRIPTION
From WorkOrderLogs e
OUTER APPLY e.SendWorkOrders.nodes('SENDWORKORDERS/WORKORDERSET/WORKORDER/WONUM') as X(Y)
It returns NULL values as my root node/namespace contains additional information of the schemas and other details. So, I'm thinking if i am able to format the value to something simple then i can achieve what i want here.
I managed write a query to pick the nodes, but failed to reference them correctly. Root nodes contain schema and additional information unique for each message. I couldn't simplify it to target the required nodes. I'm fairly new to T-SQL, so any help/pointers are highly appreciated.
Thanks in advance.
Update:
Thank you so much Yitzhak Khabinsky. It worked like a charm for one use case. But, I got another problem now. The namespace exists in child nodes.
Sample value is something like this.
<OrderNotification xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<POSKI xmlns="http://MyCompany.co.nz">12345</POSKI>
<BUKRS xmlns="http://MyCompany.co.nz">ABCD</BUKRS>
<PRCTR xmlns="http://MyCompany.co.nz">1324</PRCTR>
</OrderNotification>
Query I'm trying is
DECLARE @OrderLogs TABLE ( OrderNotification XML);
INSERT INTO @ProjectLogs (ProjectNotification)
(SELECT Info
FROM [MainLogging].[dbo].[JobLogs]
where EventId = 'XXXXXXXX'
and Message ='OrderNotification');
WITH XMLNAMESPACES (DEFAULT 'http://mycompany.co.nz')
SELECT c.value('(POSKI/text())[1]','VARCHAR(20)') AS ORDER
, c.value('(BUKRS/text())[1]','VARCHAR(256)') AS DESCRIPTION
FROM @ProjectLogs
CROSS APPLY OrderNotification.nodes('/OrderNotification') AS t(c);