更新XML标记值

时间:2017-01-05 16:47:36

标签: sql-server xml tsql xpath xquery

这是我的示例脚本。我想要做的是更新标签intstatusID和满足连接条件的xml变量@xmlPlan的strStatus

Declare @xmlPlan xml = N'<planBean>
    <intPlanID>1</intPlanID>
    <intStatusID>2</intStatusID>
    <strPlanStatus>Published</strPlanStatus>
    <strName>1 Loyalty Programs Plan</strName>
    <supplierGroupsList>
      <supplierGroup>
        <intPlanSupplierGroupID>1</intPlanSupplierGroupID>
        <intPlanID>1</intPlanID>
        <strName>Supplier Group 1</strName>
        <intStatusID>8</intStatusID>
        <strStatus>New</strStatus>
        <copySGAction>Enabled</copySGAction>
        <editSGAction>Enabled</editSGAction>
        <deleteSGAction>Enabled</deleteSGAction>
        <intActionId>5</intActionId>
        <supplierList>
          <supplier>
            <strName>HJK *</strName>
            <strSourceSupplierID>0XX135</strSourceSupplierID>
            <intStatusID>13</intStatusID>
            <strStatus>New</strStatus>
            <dtStartDate>
              <maxDate>20160101</maxDate>
              <minDate>20160101</minDate>
              <orgDate>20160101</orgDate>
            </dtStartDate>
            <dtEndDate>
              <maxDate>20161231</maxDate>
              <minDate>20161231</minDate>
              <orgDate>20161231</orgDate>
            </dtEndDate>
          </supplier>
          <supplier>
            <strName>KGH LABORATORIES LTD</strName>
            <strSourceSupplierID>0XX136</strSourceSupplierID>
            <intStatusID>13</intStatusID>
            <strStatus>New</strStatus>
            <dtStartDate>
              <maxDate>20160101</maxDate>
              <minDate>20160101</minDate>
              <orgDate>20160101</orgDate>
            </dtStartDate>
            <dtEndDate>
              <maxDate>20161231</maxDate>
              <minDate>20161231</minDate>
              <orgDate>20161231</orgDate>
            </dtEndDate>
          </supplier>        
        </supplierList>
      </supplierGroup>
      <supplierGroup>
        <intPlanSupplierGroupID>2</intPlanSupplierGroupID>
        <intPlanID>1</intPlanID>
        <strName>Supplier Group 2</strName>
        <intStatusID>8</intStatusID>
        <strStatus>New</strStatus>
        <copySGAction>Enabled</copySGAction>
        <editSGAction>Enabled</editSGAction>
        <deleteSGAction>Disabled</deleteSGAction>
        <intActionId>5</intActionId>
        <supplierList>
          <supplier>            
            <strName>ABC FOODSERVICE *</strName>
            <strSourceSupplierID>0XX134</strSourceSupplierID>
            <intStatusID>13</intStatusID>
            <strStatus>New</strStatus>
            <dtStartDate>
              <maxDate>20160101</maxDate>
              <minDate>20160101</minDate>
              <orgDate>20160101</orgDate>
            </dtStartDate>
            <dtEndDate>
              <maxDate>20161231</maxDate>
              <minDate>20161231</minDate>
              <orgDate>20161231</orgDate>
            </dtEndDate>            
          </supplier>         
        </supplierList>
      </supplierGroup>
      <supplierGroup>
        <intPlanSupplierGroupID>3</intPlanSupplierGroupID>
        <intPlanID>1</intPlanID>
        <strName>Supplier Group 3</strName>
        <intStatusID>8</intStatusID>
        <strStatus>New</strStatus>
        <copySGAction>Enabled</copySGAction>
        <editSGAction>Enabled</editSGAction>
        <deleteSGAction>Disabled</deleteSGAction>
        <intActionId>5</intActionId>
        <supplierList>
          <supplier>            
            <strName>XZ FOODSERVICE *</strName>
            <strSourceSupplierID>0XX133</strSourceSupplierID>
            <intStatusID>13</intStatusID>
            <strStatus>New</strStatus>
            <dtStartDate>
              <maxDate>20160101</maxDate>
              <minDate>20160101</minDate>
              <orgDate>20160101</orgDate>
            </dtStartDate>
            <dtEndDate>
              <maxDate>20161231</maxDate>
              <minDate>20161231</minDate>
              <orgDate>20161231</orgDate>
            </dtEndDate>            
          </supplier>         
        </supplierList>
      </supplierGroup>
    </supplierGroupsList>    
  </planBean>'

Create Table #SupplierGroups (strName varchar(500), intStatus int, strStatus varchar(200) )

Insert Into #SupplierGroups (strName, intStatus, strStatus) Values 
('Supplier Group 1', 10, 'Active'), ('Supplier Group 3', 10, 'Active')

SELECT a.*, t.c.value('(strName/text())[1]', 'varchar(500)') FROM 
@xmlPlan.nodes('planBean/supplierGroupsList/supplierGroup') t(c)
Inner Join #SupplierGroups a ON a.strName = t.c.value('(strName/text())[1]', 'varchar(500)')

- 我想要XML标签intStatusID和strStatus - 更新为[#intStatusID 10 / intStatusID,strStatus Active / strStatus]]&#34;供应商组1&#34; &#34;供应商组3&#34;只要 - 我想要@xmlPlan变量,如下所示

/*
<planBean>
    <intPlanID>1</intPlanID>
    <intStatusID>2</intStatusID>
    <strPlanStatus>Published</strPlanStatus>
    <strName>1 Loyalty Programs Plan</strName>
    <supplierGroupsList>
      <supplierGroup>
        <intPlanSupplierGroupID>1</intPlanSupplierGroupID>
        <intPlanID>1</intPlanID>
        <strName>Supplier Group 1</strName>
        <intStatusID>10</intStatusID>
        <strStatus>Active</strStatus>
        <copySGAction>Enabled</copySGAction>
        <editSGAction>Enabled</editSGAction>
        <deleteSGAction>Enabled</deleteSGAction>
        <intActionId>5</intActionId>
        <supplierList>
          <supplier>
            <strName>HJK *</strName>
            <strSourceSupplierID>0XX135</strSourceSupplierID>
            <intStatusID>13</intStatusID>
            <strStatus>New</strStatus>
            <dtStartDate>
              <maxDate>20160101</maxDate>
              <minDate>20160101</minDate>
              <orgDate>20160101</orgDate>
            </dtStartDate>
            <dtEndDate>
              <maxDate>20161231</maxDate>
              <minDate>20161231</minDate>
              <orgDate>20161231</orgDate>
            </dtEndDate>
          </supplier>
          <supplier>
            <strName>KGH LABORATORIES LTD</strName>
            <strSourceSupplierID>0XX136</strSourceSupplierID>
            <intStatusID>13</intStatusID>
            <strStatus>New</strStatus>
            <dtStartDate>
              <maxDate>20160101</maxDate>
              <minDate>20160101</minDate>
              <orgDate>20160101</orgDate>
            </dtStartDate>
            <dtEndDate>
              <maxDate>20161231</maxDate>
              <minDate>20161231</minDate>
              <orgDate>20161231</orgDate>
            </dtEndDate>
          </supplier>        
        </supplierList>
      </supplierGroup>
      <supplierGroup>
        <intPlanSupplierGroupID>2</intPlanSupplierGroupID>
        <intPlanID>1</intPlanID>
        <strName>Supplier Group 2</strName>
        <intStatusID>8</intStatusID>
        <strStatus>New</strStatus>
        <copySGAction>Enabled</copySGAction>
        <editSGAction>Enabled</editSGAction>
        <deleteSGAction>Disabled</deleteSGAction>
        <intActionId>5</intActionId>
        <supplierList>
          <supplier>            
            <strName>ABC FOODSERVICE *</strName>
            <strSourceSupplierID>0XX134</strSourceSupplierID>
            <intStatusID>13</intStatusID>
            <strStatus>New</strStatus>
            <dtStartDate>
              <maxDate>20160101</maxDate>
              <minDate>20160101</minDate>
              <orgDate>20160101</orgDate>
            </dtStartDate>
            <dtEndDate>
              <maxDate>20161231</maxDate>
              <minDate>20161231</minDate>
              <orgDate>20161231</orgDate>
            </dtEndDate>            
          </supplier>         
        </supplierList>
      </supplierGroup>
      <supplierGroup>
        <intPlanSupplierGroupID>3</intPlanSupplierGroupID>
        <intPlanID>1</intPlanID>
        <strName>Supplier Group 3</strName>
        <intStatusID>10</intStatusID>
        <strStatus>Active</strStatus>
        <copySGAction>Enabled</copySGAction>
        <editSGAction>Enabled</editSGAction>
        <deleteSGAction>Disabled</deleteSGAction>
        <intActionId>5</intActionId>
        <supplierList>
          <supplier>            
            <strName>XZ FOODSERVICE *</strName>
            <strSourceSupplierID>0XX133</strSourceSupplierID>
            <intStatusID>13</intStatusID>
            <strStatus>New</strStatus>
            <dtStartDate>
              <maxDate>20160101</maxDate>
              <minDate>20160101</minDate>
              <orgDate>20160101</orgDate>
            </dtStartDate>
            <dtEndDate>
              <maxDate>20161231</maxDate>
              <minDate>20161231</minDate>
              <orgDate>20161231</orgDate>
            </dtEndDate>            
          </supplier>         
        </supplierList>
      </supplierGroup>
    </supplierGroupsList>    
  </planBean>
*/

0 个答案:

没有答案