Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

sql - Extract value from XML not working with querying nodes or value

I'm working in SQL Server 2016, and I'm trying to extract the <Org>Insurance Technologies Corporation</Org> value from the XML below.

I've tried querying the value using nodes and value in my tsql code, but to no avail as I keep getting a NULL value for the Rater column.

I'm not sure what I'm doing wrong in either case. Any help/direction would be appreciated. Thanks.

Here is my XML:

<ACORD xmlns="http://www.ACORD.org/standards/PC_Surety/ACORD1.11.0/xml/" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <SignonRq>
    <SignonTransport>
      <SignonRoleCd>Agent</SignonRoleCd>
      <CustId>
        <SPName>turborater.com</SPName>
        <CustLoginId>99999</CustLoginId>
      </CustId>
    </SignonTransport>
    <ClientDt>2019-07-05T11:05:36</ClientDt>
    <CustLangPref>EN-US</CustLangPref>
    <ClientApp>
      <Org>Insurance Technologies Corporation</Org>
      <Name>ITC.Insurance.EmpowerAnnSvngsRR.TX</Name>
      <Version>1.0.0.0</Version>
    </ClientApp>
  </SignonRq>
 </ACORD>

Here is my tsql code using nodes:

SELECT * 
FROM (
    SELECT rtr.BridgedDate, rtr.RealTimeRequestSysId, rtr.QuoteId, rtr.AgentId, rtr.XmlRequest
        , x.m.value('(Org)[1]', 'varchar(max)') as Rater
    FROM dbo.AUT_RealTimeRequest rtr
        OUTER APPLY rtr.XmlRequest.nodes('ACORD/SignonRq/ClientApp') x(m)
    WHERE rtr.BridgedDate BETWEEN '2020-11-01 00:00:00.000' AND '2020-11-30 23:59:59.997'
) y
WHERE y.AgentId = 3
AND y.QuoteId IS NOT NULL
ORDER BY y.QuoteId asc, y.BridgedDate asc;

Here is my tsql code using value:

SELECT rtr.BridgedDate, rtr.XMLRequest, 
rtr.XMLRequest.value('(/ACORD/SignonRq/ClientApp/Org)[1]','varchar(max)') as RaterValue
FROM dbo.AUT_RealTimeRequest rtr
WHERE rtr.QuoteId IS NOT NULL
AND rtr.AgentId = 3 --21001
AND rtr.BridgedDate BETWEEN '2020-11-01 00:00:00.000' AND '2020-11-30 23:59:59.997'
ORDER BY rtr.QuoteId asc, rtr.BridgedDate asc;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're ignoring the XML namespace present in your XML data:

<ACORD xmlns="http://www.ACORD.org/standards/PC_Surety/ACORD1.11.0/xml/" 
       *****************************************************************
       This is the "default" XML namespace for this XML data

Try this:

WITH XMLNAMESPACES(DEFAULT 'http://www.ACORD.org/standards/PC_Surety/ACORD1.11.0/xml/')
    SELECT 
        rtr.BridgedDate, rtr.XMLRequest, 
        rtr.XMLRequest.value('(/ACORD/SignonRq/ClientApp/Org)[1]', 'VARCHAR(50)') AS RaterValue
    FROM 
        dbo.AUT_RealTimeRequest rtr
    WHERE 
        rtr.QuoteId IS NOT NULL
        AND rtr.AgentId = 3 --21001
        AND rtr.BridgedDate BETWEEN '2020-11-01 00:00:00.000' AND '2020-11-30 23:59:59.997'
    ORDER BY 
        rtr.QuoteId ASC, rtr.BridgedDate ASC;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...