【问题标题】:How to search for value not in an SQL querying a XML column in a table如何搜索不在查询表中 XML 列的 SQL 中的值
【发布时间】:2014-10-29 18:59:17
【问题描述】:

我有以下 SQL 查询:

SELECT
    [content_id] AS [LinkID]
    , dbo.usp_ClearHTMLTags(CONVERT(nvarchar(600), CAST([content_html] AS XML).query('root/Physicians/name'))) AS [Physician Name]
    , [content_status] AS [Status]
    , CAST ([content_html] AS XML).value('(root/Physicians/picture/img/@src)[1]','varchar(255)') AS [Image]
    , dbo.usp_ClearHTMLTags(CONVERT(nvarchar(600), CAST([content_html] AS XML).query('root/Physicians/gender'))) AS [Gender]
    , CAST ([content_html] AS XML).query('/root/Physicians/OfficeLocations/office1/a') AS [Office1]
    , dbo.usp_ClearHTMLTags(CONVERT(nvarchar(600), CAST ([content_html] as XML).query('/root/Physicians/langF3'))) AS [Lang3]
    , dbo.usp_ClearHTMLTags(CONVERT(nvarchar(600), CAST ([content_html] as XML).query('/root/Physicians/langF4'))) AS [Lang4]
    , dbo.usp_ClearHTMLTags(CONVERT(nvarchar(600), CAST ([content_html] as XML).query('/root/Physicians/langF5'))) AS [Lang5]
FROM
    [mydb].[dbo].[content]
WHERE
    (content_html LIKE '%'+@strInsurance+'%')

strInsurance 是用户正在搜索的 DropDownList 值。在当前状态下,查询会搜索任何出现的事件并将其显示在结果中。

我已经设置了一个表格,其中有两个选项,要么勾选All Insurances,要么输入医生不接受的保险。

表格截图:

来自content_html 列的示例 XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <Physicians>
        <name>Z H MD</name>
        <picture />
        <gender>M</gender>
        <langAccept />
        <insAll>false</insAll>
        <notIns1>
            <a href="/default.aspx?id=2871" title="Great West Healthcare">Great West Healthcare</a>
        </notIns1>
        <notIns2>
            <a href="/default.aspx?id=2862" title="Archcare Advantage SNP">Archcare Advantage SNP</a>
        </notIns2>
        <notIns3>
            <a href="/default.aspx?id=2877" title="Magnacare">Magnacare</a>
        </notIns3>
        <notIns4 />
        <notIns5 />
        <notIns6 />
        <notIns7 />
        <notIns8 />
        <notIns9 />
        <notIns10 />
        <specialty />
        <specialty2 />
        <specialty3 />
    </Physicians>
</root>

所以,上述医生将接受除上述三种以外的所有保险。

我想要实现的目标:如果用户选择Great West Healthcare {从下拉列表},上面的医生不会出现,但如果用户选择“蓝十字蓝盾”{从下拉列表},上面的医生会出现的。

如何修改 SQL 查询以实现该目的或使用后面的代码。

我目前有一个只显示每一列的中继器。

【问题讨论】:

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


    【解决方案1】:

    好的,这是我第一次使用 SQLFiddle 提供链接。我对此进行了测试,它对我有用。如果由于某种原因失败,我深表歉意。

    看起来您的 HTML 列是一个 varchar 列。如果将其更改为 XML 列,则可以使用 XML 查询函数。这是SQL Fiddle

    此外,如果可能,您应该重组您的 XML 以删除“notIns1”...“notIns10”元素并用一组“notIns”元素替换它们。 (类似于良好的规范化规则。)

    【讨论】:

    • 数据类型为 nvarchar。我一定会尝试让你知道 :) 谢谢。所以你建议有多个 notins 然后有 notins#?
    • 并且值可以是任何值,三个条目只是一个例子。
    • 是的,NotIns1 到 10 会人为地限制不接受的不同保险类型的数量。 (在我看来,这可能很容易超过 10 项,但我不是主题专家。)格式良好的 XML 通常有一个包含组成员的组元素。例如: 这也使得编写代码来处理 XML 变得更加困难,因为您需要寻找 10 个不同的标签名称只有一个。
    猜你喜欢
    • 2018-07-06
    • 2018-12-19
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多