我有一个xml,例如:
<AP>
<value UserXML="<ADUsers><ADUser DisplayName = "User3 (rdserver\TEST\user3)" Username="TEST\user3" DomainID="43" UserSID="ad8b3d173afa97b49b98c57b182356b6d0066f8c" UserTypeCode="INTR" /></ADUsers>" SIDHash="ad8b3d173afa97b49b98c57b182356b6d0066f8c" Type="USER" DisplayName="User3 (rdserver\TEST\user3)" Username="TEST\user3" DomainID="43" UserSID="ad8b3d173afa97b49b98c57b182356b6d0066f8c" UserTypeCode="INTR" />
<value UserXML="<ADUsers><ADUser DisplayName = "User4 (rdserver\TEST\user4)" Username="TEST\user4" DomainID="43" UserSID="7a0600b592180dba051ec99f8b8b960515d3e05f" UserTypeCode="INTR" /></ADUsers>" SIDHash="7a0600b592180dba051ec99f8b8b960515d3e05f" Type="USER" DisplayName="User4 (rdserver\TEST\user4)" Username="TEST\user4" DomainID="43" UserSID="7a0600b592180dba051ec99f8b8b960515d3e05f" UserTypeCode="INTR" />
</AP>
我有一个 xml(我的主要 xml AP 中的 ADUsers)
我想将 ADUsers xml 插入到我的 sql 数据库中的列中:
这是我的查询:
...
DECLARE @InsertedAP AS TABLE (ID INT);
DECLARE @docHandle INT
EXEC sp_xml_preparedocument @docHandle OUTPUT, @AP_List
INSERT INTO
AssociationPoints
(APType,UserXML,SIDHash)
OUTPUT INSERTED.AssociationPointID INTO @InsertedAP
SELECT
AP.[Type],
CAST(AP.UserXML as XML),
AP.SIDHash
FROM
OPENXML(@docHandle, 'AP/value', 1)
WITH
([Type] CHAR(4), UserXML XML, SIDHash VARCHAR(50)) AP
IF @@ERROR <> 0
BEGIN
ROLLBACK
RETURN
END
...
但我得到错误:当其中一列是 XML 类型时,必须使用以元素为中心的映射与 OPENXML。
为什么?我该如何解决这个问题?(内部 xml 被编码发送到数据库)
例如,我附上了可以运行的脚本:
declare @AP_List XML
set @AP_List = '<AP><value UserXML="<ADUsers><ADUser DisplayName = "User3 (rdserver\TEST\user3)" Username="TEST\user3" DomainID="43" UserSID="ad8b3d173afa97b49b98c57b182356b6d0066f8c" UserTypeCode="INTR" /></ADUsers>" SIDHash="ad8b3d173afa97b49b98c57b182356b6d0066f8c" Type="USER" DisplayName="User3 (rdserver\TEST\user3)" Username="TEST\user3" DomainID="43" UserSID="ad8b3d173afa97b49b98c57b182356b6d0066f8c" UserTypeCode="INTR"></value><value UserXML="<ADUsers><ADUser DisplayName = "User4 (rdserver\TEST\user4)" Username="TEST\user4" DomainID="43" UserSID="7a0600b592180dba051ec99f8b8b960515d3e05f" UserTypeCode="INTR" /></ADUsers>" SIDHash="7a0600b592180dba051ec99f8b8b960515d3e05f" Type="USER" DisplayName="User4 (rdserver\TEST\user4)" Username="TEST\user4" DomainID="43" UserSID="7a0600b592180dba051ec99f8b8b960515d3e05f" UserTypeCode="INTR"></value></AP>'
DECLARE @docHandle INT
EXEC sp_xml_preparedocument @docHandle OUTPUT, @AP_List
SELECT
AP.[Type],
CAST(AP.UserXML as XML),
AP.SIDHash
FROM
OPENXML(@docHandle, 'AP/value', 1)
WITH
([Type] CHAR(4), UserXML XML, SIDHash VARCHAR(50)) AP