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 に答える