這是我需要決議的 XML 輸入示例:
declare @dat XML = '
<ns:el xmlns:ns="http://dataexample/rest/entit">
<ns:ent href="/1">a - 10</ns:ent>
<ns:ent href="/2">b - 20</ns:ent>
<ns:ent href="/3">c - 30</ns:ent>
<ns:ent href="/4">d - 40</ns:ent>
<ns:ent href="/5">e - 50</ns:ent>
</ns:el>
';
with XMLNAMESPACES('http://dataexample/rest/entit' as ns)
select b.value('(.)[1]', 'nvarchar(2000)') as columna
from @dat.nodes('/ns:el') as a(b)
但是通過我的代碼,我得到了這個: a - 10b - 20c - 30d - 40e - 50 。一行,但我需要達到這一點:
Ref Name
1 a- 10
2 b - 20
3 c - 30
4 d - 40
5 e - 50
我需要在查詢中修改什么才能達到預期的結果。
uj5u.com熱心網友回復:
請嘗試以下方法。
將命名空間指定為DEFAULT可以防止在 XPath 運算式中出現命名空間前綴。
SQL
DECLARE @dat XML =
N'<ns:el xmlns:ns="http://dataexample/rest/entit">
<ns:ent href="/1">a - 10</ns:ent>
<ns:ent href="/2">b - 20</ns:ent>
<ns:ent href="/3">c - 30</ns:ent>
<ns:ent href="/4">d - 40</ns:ent>
<ns:ent href="/5">e - 50</ns:ent>
</ns:el>';
;WITH XMLNAMESPACES (DEFAULT 'http://dataexample/rest/entit')
SELECT SUBSTRING(c.value('@href', 'VARCHAR(10)'), 2, 10) AS href
, c.value('text()[1]', 'NVARCHAR(2000)') AS columna
FROM @dat.nodes('/el/ent') as t(c);
輸出
------ ---------
| href | columna |
------ ---------
| 1 | a - 10 |
| 2 | b - 20 |
| 3 | c - 30 |
| 4 | d - 40 |
| 5 | e - 50 |
------ ---------
uj5u.com熱心網友回復:
你可以試試這個:
;WITH XMLNAMESPACES ('http://dataexample/rest/entit' AS ns)
SELECT TOP 4
b.value('(.)[1]', 'varchar(50)') AS columna
FROM @dat.nodes('/ns:el/ns:ent') as a(b)
GO
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/484974.html
