【问题标题】:Update multiple columns of XML column using XQuery使用 XQuery 更新 XML 列的多个列
【发布时间】:2012-07-25 10:49:10
【问题描述】:

我有以下

第一个表

Declare @t1 Table(Id int , PaymentXML XML)

    Insert Into @t1 
    Select 1, '<CreditCard>
            <cc_display_name />
            <cc_type>MasterCard</cc_type>
            <cc_no>************5811</cc_no> 
            <cc_expire_month>4</cc_expire_month>
            <cc_expire_year>2007</cc_expire_year>         
          </CreditCard>' Union All
    Select 2 , '<CreditCard>
            <cc_display_name />
            <cc_type>MasterCard</cc_type>
            <cc_no>****1234567890</cc_no>
            <cc_expire_month>3</cc_expire_month>
            <cc_expire_year>2010</cc_expire_year>        
          </CreditCard>' Union All
    Select 3 , '<CreditCard>
            <cc_display_name />
            <cc_type>MasterCard</cc_type>
            <cc_no>****45678</cc_no>
            <cc_expire_month>10</cc_expire_month>
            <cc_expire_year>2011</cc_expire_year>        
          </CreditCard>' Union All   

    Select 4 , '<CreditCard>
            <cc_display_name />
            <cc_type>MasterCard</cc_type>
            <cc_no>****1234567890</cc_no>
            <cc_expire_month>5</cc_expire_month>
            <cc_expire_year>1997</cc_expire_year>        
          </CreditCard>'
     Select * From @t1 

第二张表

Declare @t2 Table(Id int) 
 Insert Into @t2 Select 1 Union All Select 2 
 Select * From @t2

我需要编写一个更新语句,这样对于@t1 和@t2 表的每个匹配行,PaymentXML 列节点都将更新为如下

a) 将为空白(即 )

b) 将为空白(即)

c) 将为零(0)(即 0)

d) 将为零(0)(即 0)

我提供了一个非常基本的镜头,但由于我是 xquery 新手,所以需要帮助

DECLARE @cc_type VARCHAR(10)
SELECT @cc_type = ''

Update @t1
SET PaymentXML.modify(
'
    replace value of (/CreditCard/@cc_type)[1] 
    with sql:variable("@cc_type")
')
From @t1 a
Join @t2 b On a.Id = b.Id

提前致谢

【问题讨论】:

    标签: sql-server-2008 xquery


    【解决方案1】:

    你可以像这样更新多个列

    update @t1 set 
        PaymentXML.modify('replace value of (/CreditCard/cc_type/text())[1] with ""')
    from @t1 a
        inner join @t2 b on b.Id = a.Id
    
    update @t1 set 
        PaymentXML.modify('replace value of (/CreditCard/cc_no/text())[1] with ""')
    from @t1 a
        inner join @t2 b on b.Id = a.Id
    
    update @t1 set 
        PaymentXML.modify('replace value of (/CreditCard/cc_expire_month/text())[1] with "0"')
    from @t1 a
        inner join @t2 b on b.Id = a.Id
    
    update @t1 set 
        PaymentXML.modify('replace value of (/CreditCard/cc_expire_year/text())[1] with "0"')
    from @t1 a
        inner join @t2 b on b.Id = a.Id
    

    但对于您的情况,最简单的方法是

    update @t1 set
        PaymentXML = 
            '<CreditCard>
                 <cc_display_name/>
                 <cc_type/>
                 <cc_no/>
                 <cc_expire_month>0</cc_expire_month>
                 <cc_expire_year>0</cc_expire_year>        
             </CreditCard>'
    from @t1 a
        inner join @t2 b on b.Id = a.Id
    

    【讨论】:

      猜你喜欢
      • 2011-05-29
      • 1970-01-01
      • 1970-01-01
      • 2015-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多