如何从 sql table 中的 xml 列 select 值
How to select value from xml column in sql table
我有一个 table 如下:
CREATE TABLE [dbo].[testdb](
[Id] [int] NOT NULL,
[Description] [nvarchar](4000) NULL,
CONSTRAINT [PK_testdb] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
这是值:
INSERT INTO [TempTestDb].[dbo].[testdb]
([Id]
,[Description])
VALUES
(<Id, int,>
,'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<retail:customerAttribute xmlns:core="http://www.ctor.com/core" xmlns:hta="http://docs.oasis-open.org/ns/bp4people/ws-humantask/api/200803" xmlns:htd="http://docs.oasis-open.org/ns/bp4people/ws-humantask/200803" xmlns:htt="http://docs.oasis-open.org/ns/bpel4people/ws-humantask/types/200803" xmlns:ns11="http://www.enactor.com/addressLookup/service" xmlns:ns13="http://www.ctor.com/retail/restaurantTableStatus/service" xmlns:ns4="http://www.enactor.com/crm" xmlns:ns5="http://www.ctor.com/retail/storedRetailTransaction/service" xmlns:ns7="http://www.enactor.com/retail/storedRestaurantSaleTransaction/service" xmlns:ns8="http://www.ctor.com/crm/customerLoyalty/service" xmlns:retail="http://www.ctor.com/retail" xmlns:sref="http://docs.oasis-open.org/wsbpel/2.0/serviceref" xmlns:tools="http://www.ctor.com/tools" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<retail:optionPathId>NEAREST_STORE</retail:optionPathId>
<retail:optionSetId type="customerAttributeSet" optionSetId="NEAREST_STORE">
<retail:groupId groupHierarchyId="All" groupTypeId="region">All</retail:groupId>
</retail:optionSetId>
<retail:dataType>STRING</retail:dataType>
<retail:lastUpdated>2015-03-13T09:40:59.333Z</retail:lastUpdated>
<retail:value id="NEAREST_STORE">
<retail:stringValue>001</retail:stringValue>
</retail:value>
<retail:customerId>2600009991693</retail:customerId>
</retail:customerAttribute>
')
GO
我正在尝试 select 'retail:stringValue' 的值。
我正在尝试编写的查询似乎不起作用。如果有人可以告诉我,请告诉我。
我的select查询:
SELECT TOP 1000 [Id]
,[Description].value('(/retail:value/retail:stringValue/node())[1]', 'nvarchar(max)') as test
FROM [TempTestDb].[dbo].[testdb]
假设您的列是数据类型XML
(应该是),那么您必须尊重 XML 您的 XML 中涉及的命名空间以获取值 - 试试这个代码:
;WITH XMLNAMESPACES(DEFAULT 'http://www.ctor.com/retail')
SELECT
[Id],
[Description].value('(/customerAttribute/value[1]/stringValue)[1]', 'nvarchar(100)')
FROM
[TempTestDb].[dbo].[testdb]
如果您仔细查看 XML,您会发现大多数节点都带有 retail:
XML 命名空间前缀:
<retail:customerAttribute
并且该前缀映射到 XML 命名空间 'http://www.ctor.com/retail'
- 因此在 select 到 XML 结构中,您 必须尊重 XML 命名空间并将其包含在您的 XQuery 中 - 否则您将得到 null .....
我有一个 table 如下:
CREATE TABLE [dbo].[testdb](
[Id] [int] NOT NULL,
[Description] [nvarchar](4000) NULL,
CONSTRAINT [PK_testdb] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
这是值:
INSERT INTO [TempTestDb].[dbo].[testdb]
([Id]
,[Description])
VALUES
(<Id, int,>
,'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<retail:customerAttribute xmlns:core="http://www.ctor.com/core" xmlns:hta="http://docs.oasis-open.org/ns/bp4people/ws-humantask/api/200803" xmlns:htd="http://docs.oasis-open.org/ns/bp4people/ws-humantask/200803" xmlns:htt="http://docs.oasis-open.org/ns/bpel4people/ws-humantask/types/200803" xmlns:ns11="http://www.enactor.com/addressLookup/service" xmlns:ns13="http://www.ctor.com/retail/restaurantTableStatus/service" xmlns:ns4="http://www.enactor.com/crm" xmlns:ns5="http://www.ctor.com/retail/storedRetailTransaction/service" xmlns:ns7="http://www.enactor.com/retail/storedRestaurantSaleTransaction/service" xmlns:ns8="http://www.ctor.com/crm/customerLoyalty/service" xmlns:retail="http://www.ctor.com/retail" xmlns:sref="http://docs.oasis-open.org/wsbpel/2.0/serviceref" xmlns:tools="http://www.ctor.com/tools" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<retail:optionPathId>NEAREST_STORE</retail:optionPathId>
<retail:optionSetId type="customerAttributeSet" optionSetId="NEAREST_STORE">
<retail:groupId groupHierarchyId="All" groupTypeId="region">All</retail:groupId>
</retail:optionSetId>
<retail:dataType>STRING</retail:dataType>
<retail:lastUpdated>2015-03-13T09:40:59.333Z</retail:lastUpdated>
<retail:value id="NEAREST_STORE">
<retail:stringValue>001</retail:stringValue>
</retail:value>
<retail:customerId>2600009991693</retail:customerId>
</retail:customerAttribute>
')
GO
我正在尝试 select 'retail:stringValue' 的值。 我正在尝试编写的查询似乎不起作用。如果有人可以告诉我,请告诉我。
我的select查询:
SELECT TOP 1000 [Id]
,[Description].value('(/retail:value/retail:stringValue/node())[1]', 'nvarchar(max)') as test
FROM [TempTestDb].[dbo].[testdb]
假设您的列是数据类型XML
(应该是),那么您必须尊重 XML 您的 XML 中涉及的命名空间以获取值 - 试试这个代码:
;WITH XMLNAMESPACES(DEFAULT 'http://www.ctor.com/retail')
SELECT
[Id],
[Description].value('(/customerAttribute/value[1]/stringValue)[1]', 'nvarchar(100)')
FROM
[TempTestDb].[dbo].[testdb]
如果您仔细查看 XML,您会发现大多数节点都带有 retail:
XML 命名空间前缀:
<retail:customerAttribute
并且该前缀映射到 XML 命名空间 'http://www.ctor.com/retail'
- 因此在 select 到 XML 结构中,您 必须尊重 XML 命名空间并将其包含在您的 XQuery 中 - 否则您将得到 null .....