我正在从 ntext 字段中检索 xml 格式的文本(下面一行的示例格式):
<root>
<DocInfo>
<CompanyName>Some Company</CompanyName>
<WebsiteUrl>http://www.someurl.com</WebsiteUrl>
<PrimaryServices>Benefits Administration</PrimaryServices>
<PrimaryServices>Payroll Processing</PrimaryServices>
<SecondaryServices>Background Checking</SecondaryServices>
<SecondaryServices>HR Outsourcing</SecondaryServices>
<SecondaryServices>Comp & Benefits</SecondaryServices>
<SecondaryServices>Administration</SecondaryServices>
</DocInfo>
</root>
使用这个 sql 我正在检索单个节点值:
select @xmlString = COALESCE(@xmlString + '', '') + cast(content_html as nvarchar(max)) FROM content where folder_id = 18
set @xmlString = replace(@xmlString,'<?xml version="1.0" encoding="UTF-16" standalone="yes"?>','')
set @XML = cast(@xmlString as xml)
Select
T.N.value('CompanyName[1]', 'varchar(250)') as CompanyName,
T.N.value('WebsiteUrl[1]', 'varchar(250)') as WebsiteUrl,
T.N.value('PrimaryServices[1]', 'varchar(250)') as PrimaryServices,
T.N.value('SecondaryServices[1]', 'varchar(250)') as SecondaryServices,
T.N.value('Description[1]', 'varchar(max)') as Description
from @XML.nodes('/root/DocInfo') as T(N)
这适用于单个节点值(CompanyName、WebsiteUrl)。但是,它没有正确插入具有多个值的节点(例如 PrimaryServices 和 SecondaryServices - 每个节点可能有 0 到 16 个节点)。如何将这些可变长度的多个节点值放入这些列中?
谢谢你的帮助