繁体   English   中英

XQuery存在检查中选择SQL查询

[英]XQuery exists check in select sql query

我有一个带有xml列的sql表,其中包含类似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>

在这里,我需要查询所有具有访问权限:允许许可权限:删除的 UserName值。 为此,我正在使用以下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

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

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

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

有人能给我这种情况下的xquery吗?

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小提琴演示

更新

这将给您想要的结果。

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进行此操作,请稍后再添加。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM