How to use IF/ELSE statement to update or create new xml node entry in Sql

前端 未结 3 2063
名媛妹妹
名媛妹妹 2020-12-02 00:33

Example:


    
        Lopez, Michelle MD
        Spanish
        

        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-02 01:13

    Try to delete the anchor element first and then insert the new one. It does not matter if it is there or not for the delete statement. I also provided a better way to build your new anchor element. It takes care of creating entities for characters like &.

    -- Delete the anchor node from the XML
    set @xml.modify('delete /root/StartOne/Value6/a');
    
    -- Build the XML for the new anchor node
    set @a = (
             select @locTitle as 'a/@title',
                    @locUrl as 'a/@href',
                    '_blank' as 'a/@target',
                    @locTitle as 'a'
             for xml path(''), type
             );
    
    -- Insert the new anchor node
    set @xml.modify('insert sql:variable("@a") into (/root/StartOne/Value6)[1]');
    

提交回复
热议问题