从 SQL 服务器中的 XML 列中提取值
Extracting a value from an XML column in SQL Server
我正在尝试从下面的 XML 中提取 FirstName
和 LastName
中存在的数据值,它作为字符串出现在 [=32= 的列中] 服务器 table。
<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]
本次查询returns来自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 有多个命名空间 - 总共 17 个。应该只考虑其中的两个。由于性能原因,最好不要使用名称空间通配符。
这里是如何粉碎你的 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 |
+----+-----------+----------+
假设 XML 只能包含 1 个 AccountHolderContact
实体(或类似实体),那么您可以使用 WITHXMLSPACES
和 XML 运算符 value
获取信息:
DECLARE @XML xml = '<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>';
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 ns6)
SELECT V.X.value('(ns6:Account/ns6:AccountHolderContact/entity-Person/FirstName/text())[1]','nvarchar(20)') AS FirstName,
V.X.value('(ns6:Account/ns6:AccountHolderContact/entity-Person/LastName/text())[1]','nvarchar(20)') AS FirstName
FROM(VALUES(@XML))V(X);
但是,如果数据中可能有不止一个人,则可以使用 nodes
为每个人获取 1 行(这仍会带回具有相同样本数据的一行)。如果 AccountHolderContact
是重复项,它将如下所示:
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 ns6)
SELECT A.AHC.value('(entity-Person/FirstName/text())[1]','nvarchar(20)') AS FirstName,
A.AHC.value('(entity-Person/LastName/text())[1]','nvarchar(20)') AS FirstName
FROM(VALUES(@XML))V(X)
CROSS APPLY V.X.nodes('ns6:Account/ns6:AccountHolderContact')A(AHC);
我正在尝试从下面的 XML 中提取 FirstName
和 LastName
中存在的数据值,它作为字符串出现在 [=32= 的列中] 服务器 table。
<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]
本次查询returns来自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 有多个命名空间 - 总共 17 个。应该只考虑其中的两个。由于性能原因,最好不要使用名称空间通配符。
这里是如何粉碎你的 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 |
+----+-----------+----------+
假设 XML 只能包含 1 个 AccountHolderContact
实体(或类似实体),那么您可以使用 WITHXMLSPACES
和 XML 运算符 value
获取信息:
DECLARE @XML xml = '<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>';
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 ns6)
SELECT V.X.value('(ns6:Account/ns6:AccountHolderContact/entity-Person/FirstName/text())[1]','nvarchar(20)') AS FirstName,
V.X.value('(ns6:Account/ns6:AccountHolderContact/entity-Person/LastName/text())[1]','nvarchar(20)') AS FirstName
FROM(VALUES(@XML))V(X);
但是,如果数据中可能有不止一个人,则可以使用 nodes
为每个人获取 1 行(这仍会带回具有相同样本数据的一行)。如果 AccountHolderContact
是重复项,它将如下所示:
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 ns6)
SELECT A.AHC.value('(entity-Person/FirstName/text())[1]','nvarchar(20)') AS FirstName,
A.AHC.value('(entity-Person/LastName/text())[1]','nvarchar(20)') AS FirstName
FROM(VALUES(@XML))V(X)
CROSS APPLY V.X.nodes('ns6:Account/ns6:AccountHolderContact')A(AHC);