ruya
ruya

Reputation: 55

insert an element to nth element in a xml in Sql Server

please consider this XML:

<Employees>
    <Person>
        <ID>1000</ID>
        <Name>Nima</Name>
        <LName>Agha</LName>
    </Person>
    <Person>
        <ID>1001</ID>
        <Name>Ligha</Name>
        <LName>Ligha</LName>
    </Person>
    <Person>
        <ID>1002</ID>
        <Name>Jigha</Name>
        <LName>Jigha</LName>
    </Person>
    <Person>
        <ID>1003</ID>
        <Name>Aba</Name>
        <LName>Aba</LName>
    </Person>
</Employees>

I want to write a procedure that get a number and then I insert an element to nth Person element. for example if 1 pass to my procedure I insert an element to first person element.

Upvotes: 3

Views: 6111

Answers (3)

Devart
Devart

Reputation: 121932

Try this one -

DECLARE @Param INT = 1000
DECLARE @NodeID INT = 2

DECLARE @XML XML = '
<Employees>
    <Person>
        <ID>1000</ID>
        <Name>Nima</Name>
        <LName>Agha</LName>
    </Person>
    <Person>
        <ID>1001</ID>
        <Name>Ligha</Name>
        <LName>Ligha</LName>
    </Person>
    <Person>
        <ID>1002</ID>
        <Name>Jigha</Name>
        <LName>Jigha</LName>
    </Person>
    <Person>
        <ID>1003</ID>
        <Name>Aba</Name>
        <LName>Aba</LName>
    </Person>
</Employees>'

DECLARE @SQL NVARCHAR(MAX) = '

DECLARE @XML XML = @XML_Param
DECLARE @Id INT = @Value_Param

SET @XML.modify(''insert attribute Id {sql:variable("@Id")} into (/Employees/Person)[' + CAST(@NodeID AS VARCHAR) + ']'')

SELECT @XML_Output = @XML'

DECLARE @Definition NVARCHAR(500) = 
    N'@XML_Param XML, @Value_Param INT, @Node_Param INT, @XML_Output XML OUTPUT'

DECLARE @XMLResult XML

EXEC sys.sp_executesql
      @SQL
    , @Definition
    , @XML_Param = @XML
    , @Value_Param = @Param
    , @Node_Param = 1
    , @XML_Output = @XMLResult OUTPUT

SELECT @XMLResult

Upvotes: 0

Quassnoi
Quassnoi

Reputation: 425411

DECLARE @data XML =
'
<Employees>
    <Person>
        <ID>1000</ID>
        <Name>Nima</Name>
        <LName>Agha</LName>
    </Person>
    <Person>
        <ID>1001</ID>
        <Name>Ligha</Name>
        <LName>Ligha</LName>
    </Person>
    <Person>
        <ID>1002</ID>
        <Name>Jigha</Name>
        <LName>Jigha</LName>
    </Person>
    <Person>
        <ID>1003</ID>
        <Name>Aba</Name>
        <LName>Aba</LName>
    </Person>
</Employees>
'

DECLARE @offset INT = 2
DECLARE @value VARCHAR(100) = 'newvalue'

SET @data.modify('insert <NewAttribute>{sql:variable("@value")}</NewAttribute> as last into (/Employees/Person)[sql:variable("@offset")][1]')

SELECT @data

Upvotes: 6

Dalex
Dalex

Reputation: 3625

DECLARE @AttributeValue NVARCHAr(100) = 'TestValue';
DECLARE @NodeNR INT = 3
DECLARE @XML XML = '<Employees>
    <Person>
        <ID>1000</ID>
        <Name>Nima</Name>
        <LName>Agha</LName>
    </Person>
    <Person>
        <ID>1001</ID>
        <Name>Ligha</Name>
        <LName>Ligha</LName>
    </Person>
    <Person>
        <ID>1002</ID>
        <Name>Jigha</Name>
        <LName>Jigha</LName>
    </Person>
    <Person>
        <ID>1003</ID>
        <Name>Aba</Name>
        <LName>Aba</LName>
    </Person>
</Employees>';
SET @XML.modify('insert attribute Attribute {sql:variable("@AttributeValue")} into (/Employees/Person[position()=sql:variable("@NodeNr")])[1]')

Upvotes: 4

Related Questions