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

前端 未结 3 2041
名媛妹妹
名媛妹妹 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]');
    
    0 讨论(0)
  • 2020-12-02 01:13

    i hope this will help you

    Declare @locUrl varchar(255);
    Set @locUrl = 'xyz.aspx?id=' + '444' + '';
    
    declare @xml xml;
    set @xml = '<root>
        <StartOne>
            <Value1>Lopez, Michelle MD</Value1>
            <Value2>Spanish</Value2>
            <Value3>
                <a title="49 west point" href="myloc.aspx?id=56" target="_blank">49 west point</a>
            </Value3>
            <Value4>908-783-0909</Value4>
            <Value5>
                <a title="CM" href="myspec.aspx?id=78" target="_blank">CM</a>
            </Value5>
            <Value6>
            </Value6>
        </StartOne>
    </root>';
    
    declare @chk nvarchar(max);
    -- here implementing for Value6
    set @chk = (select 
    C.value('(Value6/a/text())[1]', 'nvarchar(max)') col
    from
    @xml.nodes('/root/StartOne') as X(C))
    -- make sure here 
    select @chk;
    
    
    if @chk is null
    begin
    -- INSERT
    SET @xml.modify('       
    insert <a title="Value6" href="Value6.aspx?id=78" target="_blank">Value6</a> 
    into (/root/StartOne/Value6)[1]') 
    end
    
    else
    begin
    -- UPDATE
    Set @xml.modify('replace value of (/root/StartOne/Value6/a/@href)[1] with sql:variable("@locUrl")');
    end
    
    select @xml
    

    UPDATE: after your below comment this is the way to update dynamically

    Declare @locUrl nvarchar(255);
    Set @locUrl = 'xyz.aspx?id=' + '444' + '';
    
    declare @xml xml;
    set @xml = '<root>
        <StartOne>
            <Value1>Lopez, Michelle MD</Value1>
            <Value2>Spanish</Value2>
            <Value3>
                <a title="49 west point" href="myloc.aspx?id=56" target="_blank">49 west point</a>
            </Value3>
            <Value4>908-783-0909</Value4>
            <Value5>
                <a title="CM" href="myspec.aspx?id=78" target="_blank">CM</a>
            </Value5>
            <Value6>
            </Value6>
        </StartOne>
    </root>';
    
    declare @a  xml;
    set @a = N'<a title="' + @locUrl+ '" href="' +@locUrl+ '" target="_blank">'+@locUrl+'</a>';
    
    SET @xml.modify
    ('insert sql:variable("@a")
    into (/root/StartOne/Value6)[1]');
    
    select @xml;
    
    0 讨论(0)
  • 2020-12-02 01:40

    You might want to just try replacing:

    set @locAnchor = ('<a href="theloc.aspx?id=' + 
    @locID + '" title="' + @locTitle + '">' + 
    @locTitle + '</a>');
    

    with:

    SELECT @locAnchor = (SELECT 'theloc.aspx?id=' + @locID AS 'Value6/a/@href',
    @locTitle AS 'Value6/a/@title',
    @locTitle AS 'Value6/a'
    FOR XML PATH (''))
    

    instead of trying to dynamically create the XML

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