{"id":26428,"date":"2023-05-31T06:44:00","date_gmt":"2023-05-31T04:44:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=26428"},"modified":"2023-08-07T17:39:30","modified_gmt":"2023-08-07T15:39:30","slug":"xml-data-in-sql-server","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/t-sql\/xml-data-in-sql-server\/","title":{"rendered":"How to use XML data type in SQL Server with examples?"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\" id=\"h-practical-t-sql-scripts-examples-to-manage-xml-data-in-sql-server\"><strong><em>Practical T-SQL scripts examples to manage XML data in SQL Server. <\/em><\/strong><\/h4>\n\n\n\n<p>How to use the XML data type in SQL Server tables ? To start, XML stands for eXtensible Markup Language, it is a standard IT format for storing and exchanging data across various heterogenous platforms. T-SQL (Transact-SQL), the SQL extension from Microsoft, provides extensive support for XML data, including the ability to query, modify, and validate XML data.<\/p>\n\n\n\n\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_83 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/expert-only.com\/en\/t-sql\/xml-data-in-sql-server\/#1-create-and-fill-the-table-used-in-the-t-sql-xml-scripts\" >1. Create and fill the table used in the T-SQL XML scripts<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/expert-only.com\/en\/t-sql\/xml-data-in-sql-server\/#2-query-xml-data-with-sql-server\" >2. Query XML Data with SQL Server<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/expert-only.com\/en\/t-sql\/xml-data-in-sql-server\/#3-xml-data-modification-in-t-sql\" >3. XML Data Modification in T-SQL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/expert-only.com\/en\/t-sql\/xml-data-in-sql-server\/#4-script-example-to-query-xml-columns\" >4. Script example to query XML Columns<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/expert-only.com\/en\/t-sql\/xml-data-in-sql-server\/#5-modifying-xml-columns\" >5. Modifying XML Columns<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/expert-only.com\/en\/t-sql\/xml-data-in-sql-server\/#6-updating-xml-data-in-a-t-sql-table\" >6. Updating XML data in a T-SQL Table<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/expert-only.com\/en\/t-sql\/xml-data-in-sql-server\/#7-querying-data-in-a-t-sql-xml-table\" >7. Querying data in a T-SQL XML Table<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-1-create-and-fill-the-table-used-in-the-t-sql-xml-scripts\"><span class=\"ez-toc-section\" id=\"1-create-and-fill-the-table-used-in-the-t-sql-xml-scripts\"><\/span>1. Create and fill the table used in the T-SQL XML scripts<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let&#8217;s consider the following sample table to manage and store products for this tutorial.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE TABLE Products(\n    ProductID int PRIMARY KEY,\n    Name varchar(100),\n    Description varchar(500),\n    Price decimal(10,2),\n    CategoryID int\n);\n<\/pre>\n\n\n\n<p>Now let&#8217;s insert a few sample rows to fill your <code>Products<\/code> table with 8 entries.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">INSERT INTO Products(ProductID, Name, Description, Price, CategoryID)\nVALUES (1, 'Product 1', 'This is a description for product 1', 23.99, 10);\n\nINSERT INTO Products(ProductID, Name, Description, Price, CategoryID)\nVALUES (2, 'Product 2', 'This is a description for product 2', 45.50, 10);\n\nINSERT INTO Products(ProductID, Name, Description, Price, CategoryID)\nVALUES (3, 'Product 3', 'This is a description for product 3', 29.99, 20);\n\nINSERT INTO Products(ProductID, Name, Description, Price, CategoryID)\nVALUES (4, 'Product 4', 'This is a description for product 4', 33.00, 20);\n\nINSERT INTO Products(ProductID, Name, Description, Price, CategoryID)\nVALUES (5, 'Product 5', 'This is a description for product 5', 16.50, 30);\n\nINSERT INTO Products(ProductID, Name, Description, Price, CategoryID)\nVALUES (6, 'Product 6', 'This is a description for product 6', 20.00, 30);\n\nINSERT INTO Products(ProductID, Name, Description, Price, CategoryID)\nVALUES (7, 'Product 7', 'This is a description for product 7', 19.99, 40);\n\nINSERT INTO Products(ProductID, Name, Description, Price, CategoryID)\nVALUES (8, 'Product 8', 'This is a description for product 8', 24.99, 40);\n<\/pre>\n\n\n\n<p>These statements will create 8 products, each with a unique ID and having 5 columns: <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Product ID<\/li>\n\n\n\n<li>name<\/li>\n\n\n\n<li>description<\/li>\n\n\n\n<li>price<\/li>\n\n\n\n<li>and associated category ID.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-2-query-xml-data-with-sql-server\"><span class=\"ez-toc-section\" id=\"2-query-xml-data-with-sql-server\"><\/span>2. Query XML Data with SQL Server<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>T-SQL provides the <code>FOR XML<\/code> clause <strong>to return query results as XML<\/strong>. This can be used in various modes: RAW, AUTO, EXPLICIT, and PATH. For instance, to get all products in XML format:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT ProductID, Name, Description, Price, CategoryID\nFROM Products\nFOR XML AUTO;\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-3-xml-data-modification-in-t-sql\"><span class=\"ez-toc-section\" id=\"3-xml-data-modification-in-t-sql\"><\/span>3. XML Data Modification in T-SQL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can store XML data in a column by using the <code>xml<\/code> data type. For instance:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE TABLE ProductDetails(\n    ProductID int PRIMARY KEY,\n    Details xml\n);\n<\/pre>\n\n\n\n<p>To insert XML data in a SQL Server column, use the following script:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">INSERT INTO ProductDetails(ProductID, Details)\nVALUES (1, '&lt;details>&lt;color>Red&lt;\/color>&lt;size>M&lt;\/size>&lt;weight>1.2&lt;\/weight>&lt;\/details>')\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-4-script-example-to-query-xml-columns\"><span class=\"ez-toc-section\" id=\"4-script-example-to-query-xml-columns\"><\/span>4. Script example to query XML Columns<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>XML columns can be queried using the <code>.value<\/code>, <code>.query<\/code>, and <code>.exist<\/code> methods provided by T-SQL. For instance, to get the color of a product:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT ProductID, \n       Details.value('(\/details\/color)[1]', 'varchar(50)') as Color\nFROM ProductDetails\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-5-modifying-xml-columns\"><span class=\"ez-toc-section\" id=\"5-modifying-xml-columns\"><\/span>5. Modifying XML Columns<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>XML data can be modified using the <code>.modify<\/code> method. As an example, to update the color of a product, using the ProductID key, use and adapt a T-SQL script like this one:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">UPDATE ProductDetails\nSET Details.modify('replace value of (\/details\/color\/text())[1] with \"Blue\"')\nWHERE ProductID = 1\n<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6-updating-xml-data-in-a-t-sql-table\"><\/span>6. Updating XML data in a T-SQL Table<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Consider that we have an XML column in the Products table to store additional details about the products. Let&#8217;s add this column:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ALTER TABLE Products\nADD ProductDetails xml;\n<\/pre>\n\n\n\n<p>We could store product specifications or <a href=\"https:\/\/www.xml.com\/axml\/axml.html\" target=\"_blank\" rel=\"noreferrer noopener\">other details in XML standard format<\/a>. Let&#8217;s say we want to update the XML details for <code>ProductID<\/code> 1:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">UPDATE Products\nSET ProductDetails = '\n&lt;details>\n    &lt;color>Red&lt;\/color>\n    &lt;size>Medium&lt;\/size>\n    &lt;weight>1.2kg&lt;\/weight>\n&lt;\/details>'\nWHERE ProductID = 1;\n<\/pre>\n\n\n\n<p>With this, we can store structured XML data directly in the table and update it as needed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"7-querying-data-in-a-t-sql-xml-table\"><\/span>7. Querying data in a T-SQL XML Table<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Now that we have some XML data in our table, we may want to query this data. We can use the <code>.value<\/code> XML method in T-SQL to do this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT ProductID, \n       Name, \n       Price, \n       ProductDetails.value('(\/details\/color)[1]', 'varchar(50)') as Color, \n       ProductDetails.value('(\/details\/size)[1]', 'varchar(50)') as Size, \n       ProductDetails.value('(\/details\/weight)[1]', 'varchar(50)') as Weight\nFROM Products\nWHERE ProductID = 1;\n<\/pre>\n\n\n\n<p>This will return a row with the <code>ProductID<\/code>, <code>Name<\/code>, <code>Price<\/code>, and the values of the <code>color<\/code>, <code>size<\/code>, and <code>weight<\/code> XML nodes. Remember that working with XML data in SQL can be tricky, but it can also <strong><em>provide a lot of flexibility when you need to store structured data<\/em><\/strong> that doesn&#8217;t fit neatly into the SQL table structure. And of course when you need a flexible and changing data structure.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-conclusion-on-xml-data-in-sql-server\">Conclusion on XML data in SQL Server<\/h3>\n\n\n\n<p>That&#8217;s it for how to use XML data type in SQL Server. This short tutorial has introduced the integration and use of XML data type with SQL Server data handling is a powerful feature in T-SQL, making it easier to exchange data between different platforms and systems. Practicing these concepts with more complex examples will help you become proficient in using XML with T-SQL. To go further, let&#8217;s check our other XML tutorial with a <a href=\"https:\/\/expert-only.com\/en\/t-sql\/implicit-conversion-from-xml-to-nvarchar\/\">workaround to avoid the implicit XML conversion error<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"mTWZ7JNFDq\"><a href=\"https:\/\/expert-only.com\/en\/t-sql\/implicit-conversion-from-xml-to-nvarchar\/\">Implicit conversion from data type XML to NVARCHAR T-SQL error<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Implicit conversion from data type XML to NVARCHAR T-SQL error&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/t-sql\/implicit-conversion-from-xml-to-nvarchar\/embed\/#?secret=h20FsWesoC#?secret=mTWZ7JNFDq\" data-secret=\"mTWZ7JNFDq\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Practical T-SQL scripts examples to manage XML data in SQL Server. How to use the XML data type in SQL Server tables ? To start, XML stands for eXtensible Markup Language, it is a standard IT format for storing <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/t-sql\/xml-data-in-sql-server\/\" title=\"How to use XML data type in SQL Server with examples?\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":10874,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[454],"tags":[],"class_list":{"0":"post-26428","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-t-sql"},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/26428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=26428"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/26428\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/10874"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=26428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=26428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=26428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}