我有一個帶有 datatype 列的表XML。我想通過讀取那個 XML 列來獲取資料。
這是存盤在該列中的 XML:
<BizMsg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:asx:xsd:xasx.802.001.04 ASX_AU_CHS_comm_802_001_04_xasx_802_001_04.xsd">
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:sese.023.001.07">
<SctiesSttlmTxInstr>
<TxId>
01114|0045852600
</TxId>
</SctiesSttlmTxInstr>
</Document>
</BizMsg>
我想在<TxId>標簽內獲得價值。
我嘗試運行此查詢,但沒有得到任何結果:
DECLARE @myDoc XML
SET @myDoc = ( Select data from TableName Where Id = 56 ) // which returns XML column value from table
SELECT @myDoc.value('(/BizMsg/Document/SctiesSttlmTxInstr/TxId)[1]', 'nvarchar(max)' )
請指教 - 我做錯了什么?
uj5u.com熱心網友回復:
您忽略了檔案中定義的XML 命名空間- 這就是為什么您沒有得到任何結果......
<BizMsg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:asx:xsd:xasx.802.001.04 ASX_AU_CHS_comm_802_001_04_xasx_802_001_04.xsd">
**** this is the root level XML namespace
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:sese.023.001.07">
**** this is the XML namespace for <Document> and its children
試試這個:
WITH XMLNAMESPACES ('urn:asx:xsd:xasx.802.001.04 ASX_AU_CHS_comm_802_001_04_xasx_802_001_04.xsd' AS RootNS,
'urn:iso:std:iso:20022:tech:xsd:sese.023.001.07' AS DocNS)
SELECT
TxId = XC.value('(DocNS:SctiesSttlmTxInstr/DocNS:TxId/text())[1]', 'varchar(100)')
FROM
YourTableNameHere
CROSS APPLY
XmlDoc.nodes('/RootNS:BizMsg/DocNS:Document') AS XT(XC)
根據您的輸入,我得到以下結果:
TxId
----------------
01114|0045852600
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/415032.html
標籤:
