【问题标题】:How to select top 5 Read XML document stored in SQL Server with XML datatype?如何选择使用 XML 数据类型存储在 SQL Server 中的前 5 个读取 XML 文档?
【发布时间】:2014-04-07 13:31:55
【问题描述】:

我需要读取存储在 SQL Server 2008 R2 中的 XML 数据类型的 XML 文档。

其实我是新手。我正在使用 SQL Server 2008 和 ASP.NET 4.0。我在 SQL 端以 XML 形式存储数据表 (.NET)。

这是一个将 XML 存储在 SQL Server 中的表列中的示例。好像我有10多列,这里需要选择最后插入的4列<Data />

"really a nice solution file" 

请从下面的xml文件中帮忙

提前致谢

<Section xml:space="aligned" Paste="False" xmlns="http://schemas.microsoft.com">
<Para FontSize="11" FontWeight="Normal" FontStyle="Normal" TextAlignment="Left">
<Data Text="really a nice solution, but i have an other issue that  want to create its xml file" />
</Para>
</Section>

<Section xml:space="aligned" Paste="False" xmlns="http://schemas.microsoft.com">
<Para FontSize="11" FontWeight="Normal" FontStyle="Normal" TextAlignment="Left">
<Data Text="really a nice solution file" />
</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   |really a nice solution, but i have an other issue that  want to create its xml file| 
2   |really a nice solution, |  

【问题讨论】:

    标签: c# asp.net xml sql-server-2008


    【解决方案1】:

    假设您有一个包含 IDXmlData 列的表格(根据需要进行调整 - 在提出此类问题时您应该发布您的表格结构!),您可以使用以下内容:

    DECLARE @xmltbl TABLE (ID INT, XmlData XML)
    
    INSERT INTO @xmltbl(ID, XmlData) 
    VALUES(1, '<Section xmlns="http://schemas.microsoft.com">
    <Para FontSize="11" FontWeight="Normal" FontStyle="Normal" TextAlignment="Left">
    <Data Text="really a nice solution, but i have an other issue that  want to create its xml file" /></Para></Section>'), 
    (2, '<Section xmlns="http://schemas.microsoft.com">
    <Para FontSize="11" FontWeight="Normal" FontStyle="Normal" TextAlignment="Left">
    <Data Text="really a nice solution file" /></Para></Section> ');
    
    ;WITH XMLNAMESPACES(DEFAULT 'http://schemas.microsoft.com')
    SELECT 
        ID,
        DataText = XmlData.value('(/Section/Para/Data/@Text)[1]', 'varchar(200)')
    FROM @xmltbl
    

    我不得不从您的 XML 中删除 xml:space="aligned" Paste="False",因为它会导致错误。

    这会给你一个输出:

    ID  DataText
    1   really a nice solution, but i have an other issue that  want to create its xml file
    2   really a nice solution file
    

    如果您需要添加日期的前 5 行,那么只需将您的选择更改为:

    SELECT TOP 5 
        ID, .....
    FROM ......
    ORDER BY 
        -- order by DateAdded descending - giving you the most recent 5 rows
        DateAdded DESC    
    

    更新: 尝试适应您的表格结构 - 您的表格名称仍然未知,因此您需要 适应 dbo.YourTableName 以适应您的表格的实际情况叫!

    ;WITH XMLNAMESPACES(DEFAULT 'http://schemas.microsoft.com')
    SELECT TOP 5
        ID,
        XmlContent.value('(/Section/Para/Data/@Text)[1]', 'varchar(200)')
    FROM dbo.YourTableName
    ORDER BY CreatedDate DESC
    

    【讨论】:

    • thnaks 回复但我收到一些错误,例如 XmlData.value 不能正常我已经添加了我的表结构可以请修改查询,因为我的表结构这对我来说是一个紧急的请求谢谢 marc_s 先生
    • 如果可能的话,我非常感谢您,请根据我的表结构修改上述查询
    • SELECT TOP 5 ID,Name, XMLContent= XMLContent.value('(/Section/Para/Data/@Text)[1]', 'varchar(200)') FROM News ORDER BY DateCreated我写的DESC对吗
    • 感谢修改,但我有问题说找不到列“XMLContent”或用户定义的函数或聚合“XMlContent.value”,或者名称不明确
    • 抱歉 - 错字 - 应该是 XmlContent(不是 XmlContext
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多