XQuery 存在检查选择 sql 查询

XQuery exists check in select sql query(XQuery 存在检查选择 sql 查询)
本文介绍了XQuery 存在检查选择 sql 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个带有 xml 列的 sql 表,其中包含如下 xml 的值

I have one sql table with xml column which holds the value like below xml

<Security xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Dacl>
    <ACEInformation>
      <UserName>Authenticated Users</UserName>
      <Access>Allow</Access>
      <IsInherited>false</IsInherited>
      <ApplyTo>This object only</ApplyTo>
      <Permission>List Contents</Permission>
      <Permission>Read All Properties</Permission>
      <Permission>Read Permissions</Permission>
    </ACEInformation>
    <ACEInformation>
      <UserName>Administrator</UserName>
      <Access>Allow</Access>
      <IsInherited>false</IsInherited>
      <ApplyTo>This object only</ApplyTo>
      <Permission>Read All Properties</Permission>
      <Permission>Delete</Permission>
    </ACEInformation>
    <ACEInformation>
      <UserName>Admin2</UserName>
      <Access>Allow</Access>
      <IsInherited>false</IsInherited>
      <ApplyTo>This object only</ApplyTo>
      <Permission>Read All Properties</Permission>
      <Permission>Delete</Permission>
    </ACEInformation>
    <ACEInformation>
      <UserName>Admin2</UserName>
      <Access>Deny</Access>
      <IsInherited>false</IsInherited>
      <ApplyTo>This object only</ApplyTo>
      <Permission>Read All Properties</Permission>
      <Permission>Delete</Permission>
    </ACEInformation>
  </Dacl>
</Security>

这里我的需要是,我必须查询具有访问:允许权限:删除值的所有用户名值.为此,我正在使用以下 XQuery.

Here my need is, I have to query all UserName values who are having values Access: Allow and Permission: Delete. For that, I am using following XQuery.

select (
       select A.X.value('(UserName/text())[1]', 'nvarchar(max)')+';'
       from T.xmlColumn.nodes('/Security/Dacl/ACEInformation[Access = "Allow" and Permission = "Delete"]') as A(X)
       for xml path(''), type
       ).value('text()[1]', 'nvarchar(max)')
from myTable as T

它返回值:Administrator;Admin2 用于上述 xml.查询工作正常..

It returns the value : Administrator;Admin2 for above xml. the query is working fine..

但在这里,我希望结果只有一个条目 Administrator 而不是 Admin2..因为 Admin2 在两个不同的 ACEInformation 节点中同时具有 Access:AllowAccess: Deny 值..

But here, I expect the result only one entry Administrator not Admin2.. because Admin2 has both Access:Allow and Access: Deny values in two different ACEInformation node..

在这种情况下,我必须从结果中排除 Admin2,即使它具有 Access:Allow.

In that case, I have to exclude Admin2 from result, even though it has the Access:Allow.

谁能给我这个案例的xquery?

Can you any one give me the xquery for this case?

推荐答案

with cte as (
    select
        A.X.value('(UserName/text())[1]', 'nvarchar(max)') as UserName,
        A.X.value('(Access/text())[1]', 'nvarchar(max)') as Access
    from myTable as T
        outer apply T.xmlColumn.nodes('/Security/Dacl/ACEInformation[Permission = "Delete"]') as A(X)
)
select *
from cte as c1
where
    c1.Access = 'Allow' and
    not exists (select * from cte as c2 where c2.UserName = c1.UserName and c2.Access = 'Deny')

sql 小提琴演示

这个会给你想要的结果.

This one will give you desired result.

with cte as (
    select
        T.ID,
        A.X.value('(UserName/text())[1]', 'nvarchar(max)') as UserName,
        A.X.value('(Access/text())[1]', 'nvarchar(max)') as Access
    from myTable as T
        outer apply T.xmlColumn.nodes('/Security/Dacl/ACEInformation[Permission = "Delete"]') as A(X)
)
select
    T.ID,
    stuff(
        (
            select ',' + c1.UserName
            from cte as c1
            where
              c1.ID = T.ID and c1.Access = 'Allow' and
              not exists (select * from cte as c2 where c2.ID = T.ID and c2.UserName = c1.UserName and c2.Access = 'Deny')
            for xml path(''), type
        ).value('.', 'nvarchar(max)')
    ,1,1,'')
from myTable as T

sql 小提琴演示

我认为使用纯 XQuery 可以做到这一点,稍后尝试添加.

I think it's possible to do this with pure XQuery, try to add this later.

这篇关于XQuery 存在检查选择 sql 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Union in SQL while creating XML file(创建 XML 文件时在 SQL 中联合)
strange behavior of SQL Server when sum nodes#39;s values in XML(对 XML 中的节点值求和时 SQL Server 的奇怪行为)
How to update a SQL table column with XML data(如何使用 XML 数据更新 SQL 表列)
How To Save XML Query Results to a File(如何将 XML 查询结果保存到文件)
Extracting XML sub-tags from a clob in Oracle via SQL(通过 SQL 从 Oracle 中的 clob 中提取 XML 子标签)
installed Xampp on Windows 7 32-bit. Errors when starting(在 Windows 7 32 位上安装 Xampp.启动时的错误)