我有一个以 XML 格式存储数据的表。我需要来自两个节点的数据。
示例脚本:
select MARKUP_MESSAGE
from TABLE_NAME
where row_uno = 6599064
结果示例:
<PrebillMarkup xmlns="clr-namespace:Aderant.Query.ViewModels;assembly=Aderant.Query"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<PrebillMarkup.NewValues>
<x:String x:Key="Narrative" xml:space="preserve">Review of policies against previous policies</x:String>
</PrebillMarkup.NewValues>
<PrebillMarkup.OriginalValues>
<x:String x:Key="Narrative" xml:space="preserve">Review of policies against previous policies sdffddgsgfdfdg</x:String>
</PrebillMarkup.OriginalValues>
</PrebillMarkup>
我有两个节点
<PrebillMarkup.NewValues>
和 <PrebillMarkup.OriginalValues>
。
我希望能够从
x:String
中拉出<PrebillMarkup.NewValues>
来阅读“附加政策审查 - 测试”
所以当我运行该语句时:
select MARKUP_MESSAGE
from TABLE_NAME
where row_uno = 6599064
结果将是“附加政策审查 - 测试”
或者如果我进行另一个查询来提取
的文本值<x:String x:Key="Narrative" xml:space="preserve">
关于如何执行此操作的任何想法,因为每次我尝试都会获得更多 XML 数据,但我只需要纯文本。
谢谢大家
SQL版本:
Microsoft SQL Server 2019 - 15.0.4280.7 (X64)
Standard Edition (64-bit) on Windows Server 2019 Standard 10.0 <X64>
不确定我是否完全理解你想要做什么 - 但这是一种可能的方法:
-- define table - here, for simplicity's sake, just a table variable
DECLARE @table_name TABLE
(
ID INT NOT NULL,
Markup_Message XML
)
-- insert data into table
INSERT INTO @table_name (ID, Markup_Message)
VALUES (1, '<PrebillMarkup xmlns="clr-namespace:Aderant.Query.ViewModels;assembly=Aderant.Query"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<PrebillMarkup.NewValues>
<x:String x:Key="Narrative" xml:space="preserve">Review of policies against previous policies</x:String>
</PrebillMarkup.NewValues>
<PrebillMarkup.OriginalValues>
<x:String x:Key="Narrative" xml:space="preserve">Review of policies against previous policies sdffddgsgfdfdg</x:String>
</PrebillMarkup.OriginalValues>
</PrebillMarkup>');
-- query the data, using XQuery, to extract the "x:String"
-- from the <PrebillMarkup.NewValues> node
--
-- IMPORTANT: you need to respect and include the XML Namespaces
-- defined in your XML document into your query!
--
WITH XMLNAMESPACES(DEFAULT 'clr-namespace:Aderant.Query.ViewModels;assembly=Aderant.Query',
'http://schemas.microsoft.com/winfx/2006/xaml' AS x)
SELECT
Markup_Message.value('(/PrebillMarkup/PrebillMarkup.NewValues/x:String/text())[1]', 'varchar(500)')
FROM
@table_name
WHERE
ID = 1;
运行这个,我得到结果:
(no column name)
----------------------------------------------
Review of policies against previous policies