Renaming XML elements and attributes in XML data columns in Microsoft SQL Server using T-SQL
We've recently had an issue whereby we wanted to rename an XML attribute that is stored in an XML data column within SQL Server. There are several ways to achive this Cast the column to NVARCHAR(MAX) and perform a string replacement. This is a bit risky as you're not working with strongly typed data. Perform the update from the .NET application obtaining the data from SQL and performing the transformation in .NET. This has the advantage of having the power of .NET to perform the modification. You also get error handling as you can loop through each result and perform the XML transformation and if you detect an error you can see exactly which table row caused the issue, you can also run the whole update in a transaction. Perform the update within T-SQL using the XML methods built into SQL Server. This has the advantage of being able to be run directly within SQL Management Studio, and within a transaction. It can also therefore be run as part of the installation, with oth...