How can I convert XML data to a SQL Server table using XQuery?
Jan 04, 2025 am 12:50 AMConverting XML Data to a Table in SQL Server
In SQL Server, you can convert XML data into a tabular format using the XML data type and XQuery expressions. Here's how:
Approach 1: Using Node Value Extractor
Suppose you have an XML document with data formatted like the following:
<row> <IdInvernadero>8</IdInvernadero> <IdProducto>3</IdProducto> <IdCaracteristica1>8</IdCaracteristica1> <IdCaracteristica2>8</IdCaracteristica2> <Cantidad>25</Cantidad> <Folio>4568457</Folio> </row> <row> <IdInvernadero>3</IdInvernadero> <IdProducto>3</IdProducto> <IdCaracteristica1>1</IdCaracteristica1> <IdCaracteristica2>2</IdCaracteristica2> <Cantidad>72</Cantidad> <Folio>4568457</Folio> </row>
To convert this XML into a table, you can use the following XQuery expression:
SELECT Tbl.Col.value('IdInvernadero[1]', 'smallint'), Tbl.Col.value('IdProducto[1]', 'smallint'), Tbl.Col.value('IdCaracteristica1[1]', 'smallint'), Tbl.Col.value('IdCaracteristica2[1]', 'smallint'), Tbl.Col.value('Cantidad[1]', 'int'), Tbl.Col.value('Folio[1]', 'varchar(7)') FROM @xml.nodes('//row') Tbl(Col)
Approach 2: Using Attribute Value Extractor
Alternatively, if your XML data is formatted with attributes instead of nodes, such as:
<row IdInvernadero="8" IdProducto="3" IdCaracteristica1="8" IdCaracteristica2="8" Cantidad ="25" Folio="4568457" /> <row IdInvernadero="3" IdProducto="3" IdCaracteristica1="1" IdCaracteristica2="2" Cantidad ="72" Folio="4568457" />
You can use the following XQuery expression:
SELECT Tbl.Col.value('@IdInvernadero', 'smallint'), Tbl.Col.value('@IdProducto', 'smallint'), Tbl.Col.value('@IdCaracteristica1', 'smallint'), Tbl.Col.value('@IdCaracteristica2', 'smallint'), Tbl.Col.value('@Cantidad', 'int'), Tbl.Col.value('@Folio', 'varchar(7)') FROM @xml.nodes('//row') Tbl(Col)
This query will extract the values from the XML attributes and convert them into a tabular format.
References:
- [http://kennyshu.blogspot.com/2007/12/convert-xml-file-to-table-in-sql-2005.html](http://kennyshu.blogspot.com/2007/12/convert-xml-file-to-table-in-sql-2005.html)
- [http://msdn.microsoft.com/en-us/library/ms345117(SQL.90).aspx](http://msdn.microsoft.com/en-us/library/ms345117(SQL.90).aspx)
The above is the detailed content of How can I convert XML data to a SQL Server table using XQuery?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Reduce the use of MySQL memory in Docker

How do you alter a table in MySQL using the ALTER TABLE statement?

How to solve the problem of mysql cannot open shared library

What is SQLite? Comprehensive overview

Run MySQl in Linux (with/without podman container with phpmyadmin)

Running multiple MySQL versions on MacOS: A step-by-step guide

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?

How do I configure SSL/TLS encryption for MySQL connections?
