如何基于同一节点中不同属性值的匹配找到并更新XML文档中的节点属性值?

时间:2019-06-13 18:07:58

标签: sql-server xml tsql

给出我的T-SQL存储过程:

Declare @SearchID varchar(35)
Set @SearchID = '5280301.2019050148902.00023'

Declare @DocListXML XML  

包含以下XML数据

<JobList ListItems="7">
  <Job JobFriendlyName="EMAIL INVOICES">
    <DocumentList>
      <Document Doc="1" ID="5280301.2019050148902.00020" Date="05-03-2019" Status="NEW" />
      <Document Doc="2" ID="5280301.2019050148902.00022" Date="05-03-2019" Status="NEW" />
      <Document Doc="3" ID="5280301.2019050148902.00023" Date="05-03-2019" Status="NEW" />
      <Document Doc="4" ID="5280301.2019050104301.00055" Date="05-02-2019" Status="NEW" />
      <Document Doc="5" ID="5280301.2019050104301.00056" Date="05-02-2019" Status="NEW" />
    </DocumentList>
  </Job>
  <Job JobFriendlyName="INVOICES">
    <DocumentList>
      <Document Doc="6" ID="5280300.2019050148901.00001" Date="05-03-2019" Status="NEW" />
      <Document Doc="7" ID="5280300.2019050148901.00002" Date="05-03-2019" Status="NEW" />
    </DocumentList>
  </Job>
</JobList>

对于具有Status属性值与T-SQL本地变量{{1的值相匹配的)的节点,我需要T-SQL XML代码来更新此文档并将现有的ID值替换为“ OLD” }}。

更新后,@SearchID中的结果文档应包含:

@DocListXML

我已经找到修改/替换代码来修改“ X”“ Y”的属性值,但是在属性[b]值与局部变量匹配的节点中找不到用于修改/更新更新属性[a]值的代码

任何帮助或建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

好吧,在您决定last question之后,决定采取routes.rb: devise_for :users, ActiveAdmin::Devise.config ActiveAdmin.routes(self) active_admin.rb: config.authentication_method = :authenticate_user! config.authorization_adapter = ActiveAdmin::CanCanAdapter config.on_unauthorized_access = :access_denied config.current_user_method = :current_user config.authorization_adapter = ActiveAdmin::CanCanAdapter config.on_unauthorized_access = :access_denied config.cancan_ability_class = 'Ability' config.logout_link_path = :destroy_user_session_path config.filter_attributes = [:encrypted_password, :password, :password_confirmation] config.localize_format = :long ability.rb: class Ability include CanCan::Ability def initialize(user) user ||= User.new # Standard permissions for all users (basic and super admins) can :read, ActiveAdmin::Page, :name => 'Dashboard' can :manage, ServiceUser can :manage, SupportSession, support_worker_id: user.support_worker_id can :create, SupportSession can :manage, User, id: user.id # Can manage only their own account if user.role == 'super_admin' # Extra permissions for super admins can :manage, :all end end end Console log (rails server terminal): Started GET "/admin/login" for ::1 at 2019-06-14 08:43:23 +0100 Processing by ActiveAdmin::Devise::SessionsController#new as HTML Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms) 路线,对吗? ;-)

尝试一下。

CURSOR

-这是修改命令

--Your mockup up
Declare @SearchID varchar(35) = '5280301.2019050148902.00023';

Declare @DocListXML XML=
N'<JobList ListItems="7">
  <Job JobFriendlyName="EMAIL INVOICES">
    <DocumentList>
      <Document Doc="1" ID="5280301.2019050148902.00020" Date="05-03-2019" Status="NEW" />
      <Document Doc="2" ID="5280301.2019050148902.00022" Date="05-03-2019" Status="NEW" />
      <Document Doc="3" ID="5280301.2019050148902.00023" Date="05-03-2019" Status="NEW" />
      <Document Doc="4" ID="5280301.2019050104301.00055" Date="05-02-2019" Status="NEW" />
      <Document Doc="5" ID="5280301.2019050104301.00056" Date="05-02-2019" Status="NEW" />
    </DocumentList>
  </Job>
  <Job JobFriendlyName="INVOICES">
    <DocumentList>
      <Document Doc="6" ID="5280300.2019050148901.00001" Date="05-03-2019" Status="NEW" />
      <Document Doc="7" ID="5280300.2019050148901.00002" Date="05-03-2019" Status="NEW" />
    </DocumentList>
  </Job>
</JobList>';

-检查结果

SET @DocListXML.modify(N'replace value of (/JobList
                                           /Job
                                           /DocumentList
                                           /Document[@ID=sql:variable("@SearchID")]
                                           /@Status)[1] with "OLD"');

提示1:仅在每个XML都具有给定ID的文档的情况下,此功能才有效!

提示2:您可以将命令缩短为:

SELECT @DocListXML;

深度搜索(由XPath开头的双斜杠SET @DocListXML.modify(N'replace value of (//Document[@ID=sql:variable("@SearchID")]/@Status)[1] with "OLD"'); 触发)告诉引擎找到一个//来填充谓词在您的XML中 任何地方