How to update a SQL table column with XML data

后端 未结 1 582
忘了有多久
忘了有多久 2021-01-21 03:23

Table 1:

id      title               chtml
0       Lopez, Michelle MD  Lopez, Michelle MDS         


        
1条回答
  •  滥情空心
    2021-01-21 04:05

    this is the way to solve your problem

    declare @table4 table
    (Name nvarchar(22), Value3 nvarchar(22))
    
        insert into @table4 values ('Lopez, Michelle MD' ,'130 chester lane')       
        insert into @table4 values ('Michael, Jogn, MD ','320 nolan street') 
    
    declare @table1 table
    (id int, title nvarchar(max), chtml ntext)
    
    
    insert into @table1 values (0,'Lopez, Michelle MD',  'Lopez, Michelle MDSpanish
    49 west point908-783-0909CM')
    insert into @table1 values (1,'Michael, Jogn, MD',   'Michael, Jogn, MDEnglish99 show drive908-783-0909KM')
    
    
    declare @xml xml;
    select top 1 @xml = cast(chtml as xml)
    from @table1
    
    -- How can I update the anchor link values of one table by querying another table data.
    declare @titl nvarchar(22)
    select @titl = Value3 from @table4 where Name = 'Lopez, Michelle MD'
    set @xml.modify('
      replace value of (/root/StartOne/Value3/a/@title)[1]
      with sql:variable("@titl")
    ');
    set @xml.modify('
      replace value of (/root/StartOne/Value3/a/text())[1]
      with sql:variable("@titl")
    ');
    
    -- How can I update multiple fields, for Example `Value3` and `Value5`?
    -- Answer: here you can modify Value5
    
    update @table1
    set chtml = cast(@xml as nvarchar(max))
    where id = 0
    
    select * from @table1
    

    0 讨论(0)
提交回复
热议问题