从SQL存储过程中的xml中获取值

时间:2017-06-12 15:50:06

标签: sql sql-server xml stored-procedures

我尝试从存储过程中的XML中获取元素,如下所示

     USE [ION]
    GO
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE  [dbo].[AddToCustomer]
        (@id VARCHAR(20))
    AS
    BEGIN
        DECLARE @xx xml
        DECLARE @custname varchar(8000)
        DECLARE @fid varchar(20)
        SET NOCOUNT ON;

        SELECT @fid=[C_ID], @xx=cast([C_XML] as xml) FROM [ION].[dbo].[COR_INBOX_ENTRY] WHERE [C_WAS_PROCESSED]=0 and [C_ID]=@id

        if(@@ROWCOUNT!=0)
        BEGIN

            SELECT @xx.value('(/SyncMESCustomer/ApplicationArea/Sender/ConfirmationCode)[1]', 'varchar(100)')
        END

    END

我很确定变量@xx是用xml加载的。我需要选择'/ SyncMESCustomer / ApplicationArea / Sender / ConfirmationCode'的值,但它返回NULL。

示例xml如下

<SyncMESCustomer xmlns="http://schema.infor.com/InforOAGIS/2" xmlns:xs="http://www.w3.org/2001/XMLSchema" languageCode="GB" releaseID="9.2" systemEnvironmentCode="Production" versionID="2.9.1">
    <ApplicationArea>
        <Sender>
            <LogicalID schemeVersionID="15.4.0.0">lid://infor.m3be.ipc_mec_dev</LogicalID>
            <ComponentID schemeVersionID="MCP2-1514-02">M3BE</ComponentID>
            <ConfirmationCode>OnError</ConfirmationCode>
        </Sender>
        <CreationDateTime>2017-06-09T13:41:36.996Z</CreationDateTime>
        <BODID>5cfa6949-11ef-41dc-a380-4cc79f2687cc</BODID>
    </ApplicationArea>
    <DataArea>
        <Sync>
            <TenantID>TEST</TenantID>
            <AccountingEntityID>1_001</AccountingEntityID>
            <ActionCriteria>
                <ActionExpression actionCode="Replace" />
            </ActionCriteria>
        </Sync>
        <MESCustomer>
            <CUSTOMER_NO>TEST6</CUSTOMER_NO>
            <ipc_CSS_CUST_CUST_NAME>
                <CUSTOMER_NO>TEST6</CUSTOMER_NO>
                <CUST_NAME>Hawaii Inc_MES</CUST_NAME>
                <STATUS_DATE>20170609</STATUS_DATE>
                <STATUS_WORD>20</STATUS_WORD>
            </ipc_CSS_CUST_CUST_NAME>
            <STATUS_WORD>20</STATUS_WORD>
        </MESCustomer>
    </DataArea>
</SyncMESCustomer>

提前致谢。

2 个答案:

答案 0 :(得分:1)

;WITH XMLNAMESPACES(DEFAULT 'http://schema.infor.com/InforOAGIS/2','http://www.w3.org/2001/XMLSchema' as ns0)
Select @XML.value('SyncMESCustomer[1]/ApplicationArea[1]/Sender[1]/ConfirmationCode[1]','varchar(100)')

返回

OnError

答案 1 :(得分:0)

这对我有用......你可以在命名空间中添加另一个别名......

 <SyncMESCustomer xmlns:xz="http://schema.infor.com/InforOAGIS/2" xmlns:xs="http://www.w3.org/2001/XMLSchema" languageCode="GB" releaseID="9.2" systemEnvironmentCode="Production" versionID="2.9.1"

DECLARE @xx XML

SET @xx = '<SyncMESCustomer>
    <ApplicationArea>
        <Sender>
            <LogicalID schemeVersionID="15.4.0.0">lid://infor.m3be.ipc_mec_dev</LogicalID>
            <ComponentID schemeVersionID="MCP2-1514-02">M3BE</ComponentID>
            <ConfirmationCode>OnError</ConfirmationCode>
        </Sender>
        <CreationDateTime>2017-06-09T13:41:36.996Z</CreationDateTime>
        <BODID>5cfa6949-11ef-41dc-a380-4cc79f2687cc</BODID>
    </ApplicationArea>
    <DataArea>
        <Sync>
            <TenantID>TEST</TenantID>
            <AccountingEntityID>1_001</AccountingEntityID>
            <ActionCriteria>
                <ActionExpression actionCode="Replace" />
            </ActionCriteria>
        </Sync>
        <MESCustomer>
            <CUSTOMER_NO>TEST6</CUSTOMER_NO>
            <ipc_CSS_CUST_CUST_NAME>
                <CUSTOMER_NO>TEST6</CUSTOMER_NO>
                <CUST_NAME>Hawaii Inc_MES</CUST_NAME>
                <STATUS_DATE>20170609</STATUS_DATE>
                <STATUS_WORD>20</STATUS_WORD>
            </ipc_CSS_CUST_CUST_NAME>
            <STATUS_WORD>20</STATUS_WORD>
        </MESCustomer>
    </DataArea>
</SyncMESCustomer>'

SELECT 
    xmlData.A.value('./ConfirmationCode[1]', 'varchar(100)')
FROM @xx.nodes('/SyncMESCustomer/ApplicationArea/Sender') xmlData(A);