这是在表列中存储在SQL Server中的XML的示例。 就像我有超过10列,这里我需要选择最后插入的4列
<Section xml:space="aligned" Paste="False" xmlns="http://schemas.microsoft.com">
<Para FontSize="11" FontWeight="Normal" FontStyle="Normal" TextAlignment="Left">
<Run Text="this is the data to be select from here" />
</Para>
</Section>
这是表结构
ID Name XMLContent CreatedDate | Modified
--------|------------|------------------|------------------------|-----------------------------------------
1 | CATEGORYID |<Section xml:space="aligned" Paste="False".| |2011-04-05 12:28:15.030 |
2 | 114 |<Section xml:space="aligned" Paste="False".|2011-04-05 12:28:15.030 |
应该得到结果
1 |this is the data to be select from here|
2 |really a nice solution, |
我看过以前使用的博客,但这是针对xml数据类型的
SELECT TOP 5
ID,
XmlContent.value('(/Section/Para/Data/@Text)[1]', 'varchar(200)')
FROM dbo.YourTableName
ORDER BY CreatedDate DESC
但我需要以text数据类型存储的xml数据,请建议谢谢
以前的博客数据为空白
DECLARE @xmltbl TABLE (ID INT, XmlData XML)
INSERT INTO @xmltbl(ID, XmlData)
VALUES(1, '<Section xml:space="preserve" HasTrailingParagraphBreakOnPaste="False" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Paragraph FontSize="11" FontFamily="Portable User Interface" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" TextAlignment="Left">
<Run Text="Jubilee Financial Products is one of Europe’s largest structured product providers, working with 30 of the worlds’ largest banks. Jubilee is regulated in Ireland under MIFID and all products are regulated." />
</Paragraph></Section>'),
(2, '<Section xml:space="preserve" HasTrailingParagraphBreakOnPaste="False" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Paragraph FontSize="11" FontFamily="Portable User Interface" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" TextAlignment="Left">
<Run Text="Jubilee Financial Products is one of Europe’s largest structured product providers, working with 30 of the worlds’ largest banks. Jubilee is regulated in Ireland under MIFID and all products are regulated." />
</Paragraph></Section> ');
;WITH XMLNAMESPACES(DEFAULT 'xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation')
SELECT
ID,
DataText = XmlData.value('(/Section/Paragraph/Run/@Text)[1]', 'varchar(200)')
FROM @xmltbl
答案 0 :(得分:0)
最好的方法是将转换您的列转换为数据类型XML
,因为TEXT
已经死了,不应该再使用了:
ALTER TABLE dbo.YourTableName
ALTER COLUMN XmlContent XML
如果您无法执行此操作,则每次查询时都需要将TEXT
强制转换为XML
:
SELECT
CAST(XmlContent AS XML).value('declare namespace ns="http://schemas.microsoft.com";(/ns:Section/ns:Para/ns:Run/@Text)[1]', 'varchar(200)')
FROM dbo.YourTableHere