0

我有一个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="&lt;ADUsers&gt;&lt;ADUser DisplayName = &quot;User3   (rdserver\TEST\user3)&quot; Username=&quot;TEST\user3&quot; DomainID=&quot;43&quot; UserSID=&quot;ad8b3d173afa97b49b98c57b182356b6d0066f8c&quot; UserTypeCode=&quot;INTR&quot; /&gt;&lt;/ADUsers&gt;" SIDHash="ad8b3d173afa97b49b98c57b182356b6d0066f8c" Type="USER" DisplayName="User3   (rdserver\TEST\user3)" Username="TEST\user3" DomainID="43" UserSID="ad8b3d173afa97b49b98c57b182356b6d0066f8c" UserTypeCode="INTR"></value><value UserXML="&lt;ADUsers&gt;&lt;ADUser DisplayName = &quot;User4   (rdserver\TEST\user4)&quot; Username=&quot;TEST\user4&quot; DomainID=&quot;43&quot; UserSID=&quot;7a0600b592180dba051ec99f8b8b960515d3e05f&quot; UserTypeCode=&quot;INTR&quot; /&gt;&lt;/ADUsers&gt;" 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
4

3 回答 3

0

尝试更改OPENXML(@docHandle, 'AP/value', 1)OPENXML(@docHandle, 'AP/value', 2)

于 2013-12-18T12:44:23.963 回答
0

NVARCHAR(MAX)在模式声明中使用并XML在列列表中转换为。

SELECT
    AP.[Type],
    CAST(AP.UserXML as XML) AS UserXML,
    AP.SIDHash
FROM
    OPENXML(@docHandle, 'AP/value', 1)
    WITH
        ([Type] CHAR(4), UserXML NVARCHAR(MAX), SIDHash VARCHAR(50)) AP
于 2013-10-19T14:54:20.833 回答
0

不能说我喜欢您的方法,但是要从 xml 中取出 xml,您可以使用以下查询:

select 
    cast(T.C.value('@UserXML', 'nvarchar(max)') as xml) as UserXML
from @AP_List.nodes('AP/value') as T(C)

sql fiddle demo

于 2013-10-19T13:45:53.933 回答