Skip to main content

Working with xml data type in MS SQL server

Previously I had worked with xml data type in SQL 2005. There I was serializing the object in to xml and putting it into xml variable in SQL table. And whenever needed I would pull the xml from SQL table and create an object (deserialize). But recently I was presented with a challenge to manipulate the xml in SQL (T-SQL) itself.

In this scenario, xml text was being stored in a table as nvarchar(max). Let’s say the column name is “xmlFrag”. A function was written to return rows based on a set parameter. But for the set parameter, if the result set returns multiple rows, the function would combine the xmlFrag column (from both rows) and return one xml fragment. Below is the example.

1st row of the resultset.




Gud Day Biscuits


Cashew nuts


Honey






2nd row of the resultset.




Britannia Marie


Wheat


Barley






Then the function would combine these 2 rows data into one single xml fragment. The combined xml fragment would look like…




Gud Day Biscuits


Cashew nuts


Honey




Britannia Marie


Wheat


Barley






If you notice the highlighted node, the order of the feature is repeating (1,2,1,2). This was creating a problem in processing the nodes. And hence I was asked to make the order unique (1,2,3,4).

We had 2 approaches, one to make changes in the c# code itself. Two, make changes in the SQL function. Regarding making changes in the c# code, it would have been a huge task because this feature was being used in many places. So I decided to go ahead with changing the SQL function itself. Below are the steps followed to achieve the same.

Step 1. As we already know the function will return merged xml fragment from 2 rows. In the first step we will have to take the merged xml fragment into an xml variable and count the number of nodes in the xml fragment. So the total nodes are 4 in our case.

Step 2. After we know the total number of nodes where we have to make changes, we will have to loop through the nodes and modify the same. In the second step, we will have to use the .modify method on the xml variable. In this scenario, we actually have 2 variants. One, which node we are pointing to. Two, what value we want to replace. In both situations, the @counter variable will be used.

So in the modify method, the first part is “replace value of”. In here we will have to point to the correct node. This can be achieved by pointing to the node in the hierarchy (/Product/Item/Features/Feature) and then pointing to the attribute of the node (/@Order). Here since there are 4 nodes, we will have to point to the single node by specifying the position. This can be achieved by [position()=sql:variable(“@counter”)].

The second part (replacing with) we will have to specify the counter variable. This can be achieved by sql:variable(“@counter”). And of course we will have to increment the @counter variable.


Below is the complete solution.


DECLARE @processed_xml as xml
DECLARE @counter as INT
DECLARE @node_count as INT
-- This is the previous return statement in function
-- RETURN '' + replace(replace(@new_text_all,'',''),'','') + ''

-- New code to add sequence number to order attribute of Feature node.
set @counter = 1;
set @processed_xml = '' + replace(replace(@new_text_all,'',''),'','') + ''

-- Count of Feature Nodes in xml
SELECT @node_count = @processed_xml.query(' {count(/Product/Item/Features/Feature)}').value('count[1]','int')

WHILE @counter <= @node_count
BEGIN
SET @processed_xml.modify('replace value of ((/Product/Item/Features/Feature/@Order)[position()=sql:variable("@counter")][1])
with sql:variable("@counter")')
SET @counter = @counter + 1
END

-- New return statement for function
RETURN cast(@processed_xml as nvarchar(max))


Challenges faced were related to using SQL variable in both replace value of clause as well as with clause. The final output would look like below.




Gud Day Biscuits


Cashew nuts


Honey




Britannia Marie


Wheat


Barley






Feel free to contact me in case you need help.
-Vighnesh Bendre

Comments

Popular posts from this blog

Working with large xml files in c# .net

Working with large (huge) xml files is always a pain in the … The reason? These files can’t be loaded in to memory. On my desktop, where I have 2 gigs memory, I can’t open the file in even notepad. I was presented with a challenge recently to manipulate one such large xml file. The xml file was of 550+ MB. I know many would say I have seen bigger xml files than this. But the heart of the matter is if I can’t open 550+ MB file in notepad or in xmldocument in c#, then I can’t open any file bigger than this. And hence the logic to play with these files would remain same. The scenario: We have an xml file from which we want to remove a single node without removing its children. In the below sample xml fragment, the node has to be removed. The children nodes, must then be attached to ( node’s parent) node. One Two 100.22 GoodDay 3 4 Five 200.09 Cra

Create list view - Conditional Formatting in SharePoint Designer 2010

In this example, we are going to format a column based on certain condition. Here I already have a list called Projects. I also have workflow associated with it. So whenever I create a new item in the list, workflow status column shows ‘In Progress’. Subsequently when the workflow in completed, the workflow status column shows ‘Completed’. For demonstration purpose, I will set the background color of workflow status column to yellow when the status is ‘In Progress’ and to green when the status is ‘Completed’. In SharePoint Designer open the site on which you are working. Click on ‘Lists and Libraries’ link. Choose the ‘Projects’ list. In SharePoint Designer Navigation, choose ‘Lists and Libraries’. In the list settings page, click on ‘New’ in ‘Views’ section. Provide appropriate name for the view and click OK. After choosing list, click on ‘New’ in the Views section. Give appropriate name to the list. Now click on any column, then in the ribbon, click on List View Tools-&g

Upgrade and Migration for SharePoint Foundation 2010

      1.1 Introduction Microsoft SharePoint Foundation 2010 has been designed for scale and performance and as such requires new hardware and software requirements. There are 3 major steps while upgrading. 1. Plan and Prepare 2. Perform a database attach upgrade 3. Verify upgrade 1.2 Plan and Prepare   Before we run any process to upgrade from Windows SharePoint Services 3.0 to Microsoft SharePoint Foundation 2010, we have to determine which upgrade approach to take. In our scenario, Database Attach Upgrade seems to be appropriate approach to follow. We can upgrade the content for the environment on a separate farm. The result is that you do not upgrade any of the services or farm settings. You can upgrade the databases in any order and upgrade several databases at the same time. While each database is being upgraded, the content in that database is not available to users. 1.2.1 Upgrade Approach A database attach upgrade enables you to move to new hardware or a