通过存储过程丰富XML文档

时间:2017-05-27 12:43:32

标签: sql sql-server xml stored-procedures

我们的中间件软件接收XML文档并将其转发到另一个软件。但是,目标软件中有一个字段(ext_text_10),我不想覆盖它。

收到XML后,我想调用一个存储过程,它应查询目标数据库表以查找当前的ext_text_10值,并在传入的XML中插入一个带有结果值的新元素,然后传递给它。这样我们就可以简单地传递该字段的当前值。 下面的过程是读取XML,通过保留它的所有内容再次构建它,然后插入一个元素。

但是,它不会返回任何结果。下面是我的程序,以及我的源XML。 我希望有人可以告诉我出了什么问题。这是SQL Server 2016。

CREATE PROCEDURE [dbo].[z_ION_GetCurrentCostCenterData] (@xmlData xml)
AS
BEGIN

-- Prevent extra result sets from interfering with SELECT statements
SET NOCOUNT ON;
-- Parse XML
DECLARE @parsedXmlData int;
EXEC sp_xml_preparedocument @parsedXmlData OUTPUT, @xmlData;


DECLARE 

@tenantID varchar(50), 
@acc_entity varchar(3),
@doc_id varchar(50), 
@anl_code varchar(15), 
@anl_dim_id varchar(2), 
@lookup_code varchar(15), 
@anl_code_name varchar(50), 
@prohibit_posting varchar(1), 
@statusCode  varchar(1), 
@anl_cat_id varchar(15), 
@ext_text_6 varchar(50), 
@ext_text_7 varchar(50), 
@ext_text_8 varchar(50), 
@ext_text_9 varchar(50), 
@ext_num_1 varchar(50), 
@ext_num_2 varchar(50), 
@ext_num_3 varchar(50), 
@ext_num_4 varchar(50), 
@ext_num_5 varchar(50), 
@ext_date_1 varchar(50), 
@ext_date_2 varchar(50), 
@ext_fixed_1 varchar(50), 
@ext_fixed_2 varchar(50), 
@ext_fixed_3 varchar(50), 
@TableName NVarchar(255)

  --Get data from XML
  select 
    @tenantID = tenantID,
    @acc_entity = acc_entity,
    @doc_id = doc_id,
    @anl_code = anl_code, 
    @anl_dim_id = anl_dim_id,
    @lookup_code = lookup_code,
    @anl_code_name = anl_code_name,
    @prohibit_posting = prohibit_posting,
    @statusCode = statusCode,
    @anl_cat_id = anl_cat_id,
    @ext_text_6 = ext_text_6,
    @ext_text_7 = ext_text_7,
    @ext_text_8 = ext_text_8,
    @ext_text_9 = ext_text_9,
    @ext_num_1 = ext_num_1,
    @ext_num_2 = ext_num_2,
    @ext_num_3 = ext_num_3,
    @ext_num_4 = ext_num_4,
    @ext_num_5 = ext_num_5,
    @ext_date_1 = ext_date_1,
    @ext_date_2 = ext_date_2,
    @ext_fixed_1 = ext_fixed_1,
    @ext_fixed_2 = ext_fixed_2,
    @ext_fixed_3 = ext_fixed_3

  from OPENXML (@parsedXmlData,'DataArea',2)
  with 
  (tenantID varchar(50) 'Sync/TenantID',
    acc_entity varchar(3) 'Sync/AccountingEntityID',
    doc_id varchar(50) 'SunSystemsAnalysisCodes/DocumentID/ID',
    anl_code varchar(15) 'SunSystemsAnalysisCodes/AnalysisCode',
    anl_dim_id varchar(2) 'SunSystemsAnalysisCodes/AnalysisDimensionId',
    lookup_code varchar(15) 'SunSystemsAnalysisCodes/LookupCode',
    anl_code_name varchar(50) 'SunSystemsAnalysisCodes/Name',
    prohibit_posting varchar(1) 'SunSystemsAnalysisCodes/ProhibitPosting',
    statusCode varchar(1) 'SunSystemsAnalysisCodes/Status',
    anl_cat_id varchar(15) 'SunSystemsAnalysisCodes/AnalysisCatID/AnlCat_SHead',
    ext_text_6 varchar(50) 'SunSystemsAnalysisCodes/ExtendedAnalysis/ExtensionText6',
    ext_text_7 varchar(50) 'SunSystemsAnalysisCodes/ExtendedAnalysis/ExtensionText7',
    ext_text_8 varchar(50) 'SunSystemsAnalysisCodes/ExtendedAnalysis/ExtensionText8',
    ext_text_9 varchar(50) 'SunSystemsAnalysisCodes/ExtendedAnalysis/ExtensionText9',
    ext_num_1 varchar(50) 'SunSystemsAnalysisCodes/ExtendedAnalysis/ExtensionNumber1',
    ext_num_2 varchar(50) 'SunSystemsAnalysisCodes/ExtendedAnalysis/ExtensionNumber2',
    ext_num_3 varchar(50) 'SunSystemsAnalysisCodes/ExtendedAnalysis/ExtensionNumber3',
    ext_num_4 varchar(50) 'SunSystemsAnalysisCodes/ExtendedAnalysis/ExtensionNumber4',
    ext_num_5 varchar(50) 'SunSystemsAnalysisCodes/ExtendedAnalysis/ExtensionNumber5',
    ext_date_1 varchar(50) 'SunSystemsAnalysisCodes/ExtendedAnalysis/ExtensionDate1',
    ext_date_2 varchar(50) 'SunSystemsAnalysisCodes/ExtendedAnalysis/ExtensionDate2',
    ext_fixed_1 varchar(50) 'SunSystemsAnalysisCodes/ExtendedAnalysis/ExtensionFixed1',
    ext_fixed_2 varchar(50) 'SunSystemsAnalysisCodes/ExtendedAnalysis/ExtensionFixed2',
    ext_fixed_3 varchar(50) 'SunSystemsAnalysisCodes/ExtendedAnalysis/ExtensionFixed3'
    );


  --Create XML to return
  select 
  [EXT_TEXT_10] as 'SunSystemsAnalysisCodes/ExtendedAnalysis/ExtensionText10'

  from [dbo].[IMT_ANL_CODE_EXT]
      where [ANL_CAT_ID] = @anl_cat_id and [ANL_CODE] = @anl_code
  for XML PATH ('SunSystemsAnalysisCodes'), root('DataArea')


  EXEC sp_xml_removedocument @parsedXmlData

END

源XML:

<DataArea xmlns:my="http://schema.infor.com/InforOAGIS/2">
    <Sync>
        <TenantID>INFRA_TRN</TenantID>
        <AccountingEntityID>IMT</AccountingEntityID>
        <ActionCriteria>
            <ActionExpression actionCode="Change" />
        </ActionCriteria>
    </Sync>
    <SunSystemsAnalysisCodes>
        <DocumentID>
            <ID variationID="1495808583000">PRO 00002</ID>
        </DocumentID>
        <IONStatus>
            <Code listID="GenericStatus">Open</Code>
        </IONStatus>
        <AnalysisCode>PRO17</AnalysisCode>
        <AnalysisDimensionId>01</AnalysisDimensionId>
        <LookupCode>Informatiesyste</LookupCode>
        <Name>Informatiesysteem Relatics 10</Name>
        <ProhibitPosting>0</ProhibitPosting>
        <Status>0</Status>
        <AnalysisCatID>
            <AnlCat_SHead>COST CENTRE</AnlCat_SHead>
        </AnalysisCatID>
        <ExtendedAnalysis>
            <ExtensionText6>SVE</ExtensionText6>
            <ExtensionText7>Open</ExtensionText7>
            <ExtensionText8 />
            <ExtensionNumber1>81700</ExtensionNumber1>
            <ExtensionNumber2 />
            <ExtensionNumber3 />
            <ExtensionNumber4 />
            <ExtensionNumber5 />
            <ExtensionDate1>01012016</ExtensionDate1>
            <ExtensionDate2>31122016</ExtensionDate2>
            <ExtensionFixed1>1</ExtensionFixed1>
            <ExtensionFixed2>2</ExtensionFixed2>
            <ExtensionFixed3>2</ExtensionFixed3>
        </ExtendedAnalysis>
    </SunSystemsAnalysisCodes>
</DataArea>

1 个答案:

答案 0 :(得分:1)

如果这对您没有帮助,请回答我在您的问题下面的评论中提出的问题!

我的神奇晶球告诉我,你可能正在寻找类似的东西(为简洁而缩短):

DECLARE @xml XML=
N'<DataArea xmlns:my="http://schema.infor.com/InforOAGIS/2">
    <Sync>
        <TenantID>INFRA_TRN</TenantID>
        <!-- more elements -->
    </Sync>
    <SunSystemsAnalysisCodes>
        <DocumentID>
            <ID variationID="1495808583000">PRO 00002</ID>
        </DocumentID>
        <!-- more elements -->
        <ExtendedAnalysis>
            <ExtensionText6>SVE</ExtensionText6>
            <ExtensionText7>Open</ExtensionText7>
            <ExtensionText8 />
            <ExtensionNumber1>81700</ExtensionNumber1>
            <ExtensionNumber2 />
            <ExtensionNumber3 />
            <ExtensionNumber4 />
            <ExtensionNumber5 />
            <ExtensionDate1>01012016</ExtensionDate1>
            <ExtensionDate2>31122016</ExtensionDate2>
            <ExtensionFixed1>1</ExtensionFixed1>
            <ExtensionFixed2>2</ExtensionFixed2>
            <ExtensionFixed3>2</ExtensionFixed3>
        </ExtendedAnalysis>
    </SunSystemsAnalysisCodes>
</DataArea>';

- 这是您要介绍的内容

DECLARE @ContentToAdd NVARCHAR(MAX)=N'SunSystemsAnalysisCodes/ExtendedAnalysis/ExtensionText10';

- 此XML_DML - 语句会在<ExtensionNumber1>之前插入具有指定名称的内容(必须存在!其他职位当然可以

SET @xml.modify(N'insert <ExtensionText10>{sql:variable("@ContentToAdd")}</ExtensionText10> 
                  before (/DataArea/SunSystemsAnalysisCodes/ExtendedAnalysis/ExtensionNumber1)[1]');

- 检查输出:

SELECT @xml;

<DataArea xmlns:my="http://schema.infor.com/InforOAGIS/2">
  <Sync>
    <TenantID>INFRA_TRN</TenantID>
    <!-- more elements -->
  </Sync>
  <SunSystemsAnalysisCodes>
    <DocumentID>
      <ID variationID="1495808583000">PRO 00002</ID>
    </DocumentID>
    <!-- more elements -->
    <ExtendedAnalysis>
      <ExtensionText6>SVE</ExtensionText6>
      <ExtensionText7>Open</ExtensionText7>
      <ExtensionText8 />
      <ExtensionText10>SunSystemsAnalysisCodes/ExtendedAnalysis/ExtensionText10</ExtensionText10>
      <ExtensionNumber1>81700</ExtensionNumber1>
      <ExtensionNumber2 />
      <ExtensionNumber3 />
      <ExtensionNumber4 />
      <ExtensionNumber5 />
      <ExtensionDate1>01012016</ExtensionDate1>
      <ExtensionDate2>31122016</ExtensionDate2>
      <ExtensionFixed1>1</ExtensionFixed1>
      <ExtensionFixed2>2</ExtensionFixed2>
      <ExtensionFixed3>2</ExtensionFixed3>
    </ExtendedAnalysis>
  </SunSystemsAnalysisCodes>
</DataArea>