Вы можете использовать nodes
, чтобы разделить ваш XML по значению элемента, а затем value
, чтобы извлечь нужные значения:
declare @table table (MY_XML xml)
insert into @table select
'<DN_CHARGE_LINES>
<DN_CHARGE_LINE LINE_ID="201903297661150">
<CHARGE_NAME>HANDLING CHARGE</CHARGE_NAME>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<RATE>75</RATE>
<QTY>1</QTY>
<LINE_AMOUNT>75</LINE_AMOUNT>
<JOB_ID>201903137354913</JOB_ID>
</DN_CHARGE_LINE>
</DN_CHARGE_LINES>'
select
cn.s.value('@LINE_ID' , 'nvarchar(max)') AS LINE_ID
,cn.s.value('CHARGE_NAME[1]' , 'nvarchar(max)') AS CHARGE_NAME
,cn.s.value('CURRENCY_CODE[1]', 'nvarchar(max)') AS CURRENCY_CODE
,cn.s.value('RATE[1]' , 'nvarchar(max)') AS RATE
,cn.s.value('QTY[1]' , 'nvarchar(max)') AS QTY
,cn.s.value('JOB_ID[1]' , 'nvarchar(max)') AS JOB_ID
from @table
CROSS APPLY MY_XML.nodes('//DN_CHARGE_LINE') cn(s)
результат:
Если у вас есть больше <DN_CHARGE_LINE>
элементов:
declare @table table (MY_XML xml)
insert into @table select
'<DN_CHARGE_LINES>
<DN_CHARGE_LINE LINE_ID="201903297661150">
<CHARGE_NAME>HANDLING CHARGE</CHARGE_NAME>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<RATE>75</RATE>
<QTY>1</QTY>
<LINE_AMOUNT>75</LINE_AMOUNT>
<JOB_ID>201903137354913</JOB_ID>
</DN_CHARGE_LINE>
<DN_CHARGE_LINE LINE_ID="123123123123123">
<CHARGE_NAME>HANDLING CHARGE 2</CHARGE_NAME>
<CURRENCY_CODE>EUR</CURRENCY_CODE>
<RATE>100</RATE>
<QTY>2</QTY>
<LINE_AMOUNT>10</LINE_AMOUNT>
<JOB_ID>201903137565987</JOB_ID>
</DN_CHARGE_LINE>
<DN_CHARGE_LINE LINE_ID="123456789123456">
<CHARGE_NAME>HANDLING CHARGE 3</CHARGE_NAME>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<RATE>3</RATE>
<QTY>4</QTY>
<LINE_AMOUNT>30</LINE_AMOUNT>
<JOB_ID>201903137111222</JOB_ID>
</DN_CHARGE_LINE>
</DN_CHARGE_LINES>'
select
cn.s.value('@LINE_ID' , 'nvarchar(max)') AS LINE_ID
,cn.s.value('CHARGE_NAME[1]' , 'nvarchar(max)') AS CHARGE_NAME
,cn.s.value('CURRENCY_CODE[1]', 'nvarchar(max)') AS CURRENCY_CODE
,cn.s.value('RATE[1]' , 'nvarchar(max)') AS RATE
,cn.s.value('QTY[1]' , 'nvarchar(max)') AS QTY
,cn.s.value('JOB_ID[1]' , 'nvarchar(max)') AS JOB_ID
from @table
CROSS APPLY MY_XML.nodes('//DN_CHARGE_LINE') cn(s)
это результат: