Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update an xml attribute value in an xml variable using t-sql?

Let's have a sample snippet:

DECLARE @xml XML = N'
<a abb="122">
    <b>
    </b>
</a>
';
SELECT @xml;

--need to update abb to be 344 in @xml here

SELECT @xml;

I don't know how to update that attribute abb's value.

like image 999
Nam G VU Avatar asked Mar 17 '11 11:03

Nam G VU


People also ask

How do I update XML content in SQL?

To update data in an XML column, use the SQL UPDATE statement. Include a WHERE clause when you want to update specific rows. The entire column value will be replaced. The input to the XML column must be a well-formed XML document.

How can we store XML data in variable in SQL Server?

Create columns and variables CREATE TABLE T1(Col1 int primary key, Col2 xml); You can use a DECLARE statement to create a variable of xml type, as the following example shows. DECLARE @x xml; Create a typed xml variable by specifying an XML schema collection, as shown in the following example.

How do I select a value from XML in SQL?

A.The value() method retrieves the ProductID attribute value from the XML. The value is then assigned to an int variable. Value 1 is returned as a result.

Which function do you use to insert update and delete data from XML based input?

The modify() is the DML based function with an argument of XPATH to perform insert, update or delete the elements or attributes with the XML based column in the table.


1 Answers

set @xml.modify('replace value of (/a/@abb)[1] with 344')

Read more about it here. XML Data Modification Language (XML DML)

like image 75
Mikael Eriksson Avatar answered Nov 16 '22 00:11

Mikael Eriksson