1

我有以下

第一个表

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

提前致谢

4

1 回答 1

0

那么你可以像这样更新多个列

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
于 2012-12-03T11:43:14.880 回答