gpt4 book ai didi

sql-server - 从 SQL Server 中的 XML 列中提取值

转载 作者:行者123 更新时间:2023-12-04 01:24:22 31 4
gpt4 key购买 nike

我正在尝试提取 FirstName 中存在的数据值和 LastName来自下面的 XML,它在 SQL Server 表的列中作为字符串出现。

<ns6:Account xmlns="http://example.com/pc/gx/abc.pc.dm.gx.shared.contact.contactmodel" xmlns:ns2="http://example.com/pc/gx/abc.pc.dm.gx.shared.location.addressmodel" xmlns:ns4="http://example.com/pc/gx/abc.pc.dm.gx.shared.contact.officialidmodel" xmlns:ns3="http://example.com/pc/gx/abc.pc.dm.gx.shared.contact.contactaddressmodel" xmlns:ns6="http://example.com/pc/gx/abc.pc.dm.gx.base.account.abc" xmlns:ns5="http://example.com/pc/gx/abc.pc.dm.gx.shared.contact.accountcontactmodel" xmlns:ns8="http://example.com/pc/gx/abc.pc.dm.gx.shared.general.usermodel" xmlns:ns7="http://example.com/pc/gx/abc.pc.dm.gx.shared.general.historymodel" xmlns:ns13="http://example.com/pc/gx/abc.pc.dm.gx.base.account.abc" xmlns:ns9="http://example.com/pc/gx/abc.pc.dm.gx.shared.general.activitymodel" xmlns:ns12="http://example.com/pc/gx/abc.pc.dm.gx.shared.general.industrycodemodel" xmlns:ns11="http://example.com/pc/gx/abc.pc.dm.gx.shared.general.documentmodel" xmlns:ns10="http://example.com/pc/gx/abc.pc.dm.gx.shared.general.groupmodel" xmlns:ns17="http://example.com/pc/gx/abc.pc.dm.gx.shared.producer.producercodemodel" xmlns:ns16="http://example.com/pc/gx/abc.pc.dm.gx.shared.producer.accountproducercodemodel" xmlns:ns15="http://example.com/pc/gx/abc.pc.dm.gx.shared.general.notemodel" xmlns:ns14="http://example.com/pc/gx/abc.pc.dm.gx.base.account.abc">
<ns6:AccountHolderContact>
<entity-Person>
<DateOfBirth>999-01-02T12:00:00-05:00</DateOfBirth>
<FirstName>ABC</FirstName>
<Gender>F</Gender>
<LastName>ABC</LastName>
<LicenseNumber>9999-9999-9999</LicenseNumber>
<LicenseState>AA</LicenseState>
<MaritalStatus>S</MaritalStatus>
<OrganizationType_IC>individual</OrganizationType_IC>
</entity-Person>
<HomePhone>9999999999</HomePhone>
<PrimaryAddress>
<ns2:AddressLine1>99 ABC St</ns2:AddressLine1>
<ns2:AddressType>home</ns2:AddressType>
<ns2:City>AAA</ns2:City>
<ns2:Country>AA</ns2:Country>
<ns2:PostalCode>ABC MMM</ns2:PostalCode>
<ns2:State>AA</ns2:State>
<ns2:Subtype>Address</ns2:Subtype>
</PrimaryAddress>
<PublicID>1</PublicID>
<Subtype>person</Subtype>
</ns6:AccountHolderContact>
</ns6:Account>

这是我试过的查询:

select 
application_id, accountID,
cast(payload as xml).value('(//*:Account//*:AccountHolderContact)[1]', 'varchar(max)') as FirstName
from
[test1].[dbo].[test2]

此查询从 XML 节点 <AccountHolderContact> 返回所有子节点中的数据.

999-01-02T12:00:00-05:00ABCFABC9999-9999-9999AASIndividual999999999999 ABC SthomeAAAAAABC MMMAAAddress1Person

当我将查询更改为以下内容时,输出列中没有数据 FirstName :

select 
application_id, accountID,
cast(payload as xml).value('(//*:Account//*:AccountHolderContact/entity-Person/FirstName)[1]','varchar(max)') as FirstName
from
[test1].[dbo].[test2]

我不能从AccountHolderContact的子节点中提取是有原因的吗? ?如果没有,最简单的方法是什么?

最佳答案

您的 XML 有多个 namespace - 总共 17 个。应该只考虑其中的两个。由于性能原因,最好不要使用 namespace 通配符。

这里是如何分解您的 XML 并检索您需要的内容。

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, payload NVARCHAR(MAX));
INSERT INTO @tbl (payload) VALUES
(N'<?xml version="1.0"?>
<ns6:Account xmlns="http://example.com/pc/gx/abc.pc.dm.gx.shared.contact.contactmodel"
xmlns:ns2="http://example.com/pc/gx/abc.pc.dm.gx.shared.location.addressmodel"
xmlns:ns4="http://example.com/pc/gx/abc.pc.dm.gx.shared.contact.officialidmodel"
xmlns:ns3="http://example.com/pc/gx/abc.pc.dm.gx.shared.contact.contactaddressmodel"
xmlns:ns6="http://example.com/pc/gx/abc.pc.dm.gx.base.account.abc"
xmlns:ns5="http://example.com/pc/gx/abc.pc.dm.gx.shared.contact.accountcontactmodel"
xmlns:ns8="http://example.com/pc/gx/abc.pc.dm.gx.shared.general.usermodel"
xmlns:ns7="http://example.com/pc/gx/abc.pc.dm.gx.shared.general.historymodel"
xmlns:ns13="http://example.com/pc/gx/abc.pc.dm.gx.base.account.abc"
xmlns:ns9="http://example.com/pc/gx/abc.pc.dm.gx.shared.general.activitymodel"
xmlns:ns12="http://example.com/pc/gx/abc.pc.dm.gx.shared.general.industrycodemodel"
xmlns:ns11="http://example.com/pc/gx/abc.pc.dm.gx.shared.general.documentmodel"
xmlns:ns10="http://example.com/pc/gx/abc.pc.dm.gx.shared.general.groupmodel"
xmlns:ns17="http://example.com/pc/gx/abc.pc.dm.gx.shared.producer.producercodemodel"
xmlns:ns16="http://example.com/pc/gx/abc.pc.dm.gx.shared.producer.accountproducercodemodel"
xmlns:ns15="http://example.com/pc/gx/abc.pc.dm.gx.shared.general.notemodel"
xmlns:ns14="http://example.com/pc/gx/abc.pc.dm.gx.base.account.abc">
<ns6:AccountHolderContact>
<entity-Person>
<DateOfBirth>999-01-02T12:00:00-05:00</DateOfBirth>
<FirstName>ABC</FirstName>
<Gender>F</Gender>
<LastName>ABC</LastName>
<LicenseNumber>9999-9999-9999</LicenseNumber>
<LicenseState>AA</LicenseState>
<MaritalStatus>S</MaritalStatus>
<OrganizationType_IC>individual</OrganizationType_IC>
</entity-Person>
<HomePhone>9999999999</HomePhone>
<PrimaryAddress>
<ns2:AddressLine1>99 ABC St</ns2:AddressLine1>
<ns2:AddressType>home</ns2:AddressType>
<ns2:City>AAA</ns2:City>
<ns2:Country>AA</ns2:Country>
<ns2:PostalCode>ABC MMM</ns2:PostalCode>
<ns2:State>AA</ns2:State>
<ns2:Subtype>Address</ns2:Subtype>
</PrimaryAddress>
<PublicID>1</PublicID>
<Subtype>person</Subtype>
</ns6:AccountHolderContact>
</ns6:Account>');
-- DDL and sample data population, end

;WITH XMLNAMESPACES (DEFAULT 'http://example.com/pc/gx/abc.pc.dm.gx.shared.contact.contactmodel'
, 'http://example.com/pc/gx/abc.pc.dm.gx.base.account.abc' AS ns14) ,rs AS
(
SELECT id, TRY_CAST(payload AS XML) AS xmldata
FROM @tbl
)
SELECT ID
, c.value('(FirstName/text())[1]','VARCHAR(50)') AS FirstName
, c.value('(LastName/text())[1]','VARCHAR(50)') AS LastName
FROM rs CROSS APPLY rs.xmldata.nodes('/ns14:Account/ns14:AccountHolderContact/entity-Person') AS t(c);

Output

+----+-----------+----------+
| ID | FirstName | LastName |
+----+-----------+----------+
| 1 | ABC | ABC |
+----+-----------+----------+

关于sql-server - 从 SQL Server 中的 XML 列中提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62154559/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com