我正在尝试使用SQLXML修改功能来更新多值(xs:list)属性。在构造XML(从字符串)时,我可以设置多个值,但是SQLXML修改不允许我设置多个值。
初始XML:
<AccessControlList xmlns="http://www.acme.com/Authorization/2013/01">
<AccessControlRecord Permissions="Fullcontrol" />
<AccessControlRecord Permissions="DenyCreate DenyRead DenyUpdate DenyDelete" />
</AccessControlList>
设置单个值可以正常工作:
DECLARE @SingleValue NVARCHAR(100) = 'DenyCreate';
WITH XMLNAMESPACES ( 'http://www.acme.com/Authorization/2013/01' AS A )
UPDATE dbo.Widget
SET ACL.modify('replace value of (/A:AccessControlList/A:AccessControlRecord/@Permissions)[1] with sql:variable("@SingleValue") cast as A:AccessPermissions ?')
FROM dbo.Widget;
设置多个值失败:
DECLARE @MultipleValues NVARCHAR(100) = 'DenyCreate DenyRead DenyUpdate DenyDelete';
WITH XMLNAMESPACES ( 'http://www.acme.com/Authorization/2013/01' AS A )
UPDATE dbo.Widget
SET ACL.modify('replace value of (/A:AccessControlList/A:AccessControlRecord/@Permissions)[1] with sql:variable("@MultipleValues") cast as A:AccessPermissions ?')
FROM dbo.Widget;
出现此错误:
XQuery: Replacing the value of a node with an empty sequence is allowed only if '()' is used as the new value expression. The new value expression evaluated to an empty sequence but it is not '()'.
变量不为null或为空。我还尝试了其他变体,但均以不同的错误失败。
要重现的完整SQL:
-- Drop table and schema collection
IF OBJECT_ID('dbo.Widget') IS NOT NULL
DROP TABLE dbo.Widget;
IF EXISTS ( SELECT * FROM sys.xml_schema_collections WHERE SCHEMA_NAME(schema_id) = 'dbo' AND name = 'AccessControlList' )
DROP XML SCHEMA COLLECTION dbo.AccessControlList;
GO
-- Create schema collection
CREATE XML SCHEMA COLLECTION dbo.AccessControlList AS N'
<xs:schema id="AccessControlList" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.acme.com/Authorization/2013/01" xmlns="http://www.acme.com/Authorization/2013/01">
<xs:simpleType name="AccessPermissions">
<xs:list>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Create" />
<xs:enumeration value="Read" />
<xs:enumeration value="Update" />
<xs:enumeration value="Delete" />
<xs:enumeration value="Execute" />
<xs:enumeration value="Fullcontrol" />
<xs:enumeration value="DenyCreate" />
<xs:enumeration value="DenyRead" />
<xs:enumeration value="DenyUpdate" />
<xs:enumeration value="DenyDelete" />
<xs:enumeration value="DenyExecute" />
<xs:enumeration value="FullDeny" />
</xs:restriction>
</xs:simpleType>
</xs:list>
</xs:simpleType>
<xs:complexType name="AccessControlRecord">
<xs:attribute name="Permissions" type="AccessPermissions" use="required" />
</xs:complexType>
<xs:complexType name="AccessControlList">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="AccessControlRecord" type="AccessControlRecord" />
</xs:sequence>
</xs:complexType>
<xs:element name="AccessControlList" nillable="true" type="AccessControlList" />
</xs:schema>
';
GO
-- Create table, insert test data, and display initial state of data
CREATE TABLE dbo.Widget
(
WidgetId INT PRIMARY KEY IDENTITY(1,1),
ACL XML(DOCUMENT dbo.AccessControlList)
);
INSERT INTO dbo.Widget
( ACL )
VALUES
( N'<AccessControlList xmlns="http://www.acme.com/Authorization/2013/01" >
<AccessControlRecord Permissions="Fullcontrol" />
<AccessControlRecord Permissions="DenyCreate DenyRead DenyUpdate DenyDelete" />
</AccessControlList>' );
WITH XMLNAMESPACES ( 'http://www.acme.com/Authorization/2013/01' AS A )
SELECT *
,Acr1Permissions = CAST(ACL AS XML).value('(/A:AccessControlList/A:AccessControlRecord)[1]/@Permissions', 'NVARCHAR(128)')
,Acr2Permissions = CAST(ACL AS XML).value('(/A:AccessControlList/A:AccessControlRecord)[2]/@Permissions', 'NVARCHAR(128)')
FROM dbo.Widget;
-- Setting a SINGLE value works fine
DECLARE @SingleValue NVARCHAR(100) = 'DenyCreate';
WITH XMLNAMESPACES ( 'http://www.acme.com/Authorization/2013/01' AS A )
UPDATE dbo.Widget
SET ACL.modify('replace value of (/A:AccessControlList/A:AccessControlRecord/@Permissions)[1] with sql:variable("@SingleValue") cast as A:AccessPermissions ?')
FROM dbo.Widget;
-- Display values after
WITH XMLNAMESPACES ( 'http://www.acme.com/Authorization/2013/01' AS A )
SELECT *
,Acr1Permissions = CAST(ACL AS XML).value('(/A:AccessControlList/A:AccessControlRecord)[1]/@Permissions', 'NVARCHAR(128)')
,Acr2Permissions = CAST(ACL AS XML).value('(/A:AccessControlList/A:AccessControlRecord)[2]/@Permissions', 'NVARCHAR(128)')
FROM dbo.Widget;
/* Setting MULTIPLE values *FAILS*
DECLARE @MultipleValues NVARCHAR(100) = 'DenyCreate DenyRead DenyUpdate DenyDelete';
WITH XMLNAMESPACES ( 'http://www.acme.com/Authorization/2013/01' AS A )
UPDATE dbo.Widget
SET ACL.modify('replace value of (/A:AccessControlList/A:AccessControlRecord/@Permissions)[1] with sql:variable("@MultipleValues") cast as A:AccessPermissions ?')
FROM dbo.Widget;
*/
当我尝试使用“ sql:column”设置多个值时,也会遇到相同的失败。
我发现此资源(https://docs.microsoft.com/en-us/sql/xquery/type-casting-rules-in-xquery?view=sql-server-2017)表示不允许对列表类型进行强制转换;我希望有解决方案或解决方法。
使用SQLXML是否可以?怎么样?
预先感谢
答案 0 :(得分:1)
我必须承认,我以前从未处理过...
我必须承认,我没有找到简单的解决方案。如果找到它,请告诉我。
即使使用文字,也会导致相同的问题:将字符串整体转换为枚举 ,该枚举与允许的值之一不匹配,因此返回为空。
但是你可以做
replace value of (/A:AccessControlList/A:AccessControlRecord/@Permissions)[1]
with for $p in ("DenyCreate","DenyUpdate","DenyDelete")
return $p cast as A:AccessPermissions ?
这将使用XQuery for
遍历列表,并逐个返回每个值 ,每个值分别转换为所需的类型。
但是我发现将其与外部变量一起使用的唯一方法是动态SQL。因此这可行,但是很丑陋:
DECLARE @MultipleValues VARCHAR(100)='DenyCreate DenyUpdate DenyDelete';
DECLARE @cmd NVARCHAR(MAX)=
'WITH XMLNAMESPACES ( ''http://www.acme.com/Authorization/2013/01'' AS A )
UPDATE dbo.Widget
SET ACL.modify(''replace value of (/A:AccessControlList/A:AccessControlRecord/@Permissions)[1] with for $p in ("' + REPLACE(@MultipleValues,' ','","') + '") return $p cast as A:AccessPermissions ?'')
FROM dbo.Widget;';
EXEC(@cmd);