我有一个带有 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:Allow 和 Access: 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 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
获取所有产品、类别和元数据的 SQL 查询 woocommSQL query to get all products, categories and meta data woocommerce/wordpress(获取所有产品、类别和元数据的 SQL 查询 woocommerce/wordpre
我可以在不编写 SQL 查询的情况下找出数据库列表Can I figure out a list of databases and the space used by SQL Server instances without writing SQL queries?(我可以在不编写 SQL 查询的情况下
如何创建对 SQL Server 实例的登录?How to create a login to a SQL Server instance?(如何创建对 SQL Server 实例的登录?)
如何通过注册表搜索知道SQL Server的版本和版本How to know the version and edition of SQL Server through registry search(如何通过注册表搜索知道SQL Server的版本和版本)
为什么会出现“数据类型转换错误"?使用 ExWhy do I get a quot;data type conversion errorquot; with ExecuteNonQuery()?(为什么会出现“数据类型转换错误?使用 ExecuteNonQuery()?)
如何将 DataGridView 中的图像显示到 PictureBox?How to show an image from a DataGridView to a PictureBox?(如何将 DataGridView 中的图像显示到 PictureBox?)