Home Database Mysql Tutorial 把XML数据插入到SQL Server数据库的表中

把XML数据插入到SQL Server数据库的表中

Jun 07, 2016 pm 03:30 PM
server sql xml insert data database in the table

6.3 把XML数据插入到SQL Server数据库的表中 我们可以通过使用系统存储过程sp_xml_preparedocument的OPENXML函数把XML文档中的数据插入数据库中。其中,系统存储过程sp_xml_preparedocument用来创建一个能被插入数据库的XML文档的内部表示,该存储过程返回一

6.3  把XML数据插入到SQL Server数据库的表中

我们可以通过使用系统存储过程sp_xml_preparedocument的OPENXML函数把XML文档中的数据插入数据库中。其中,系统存储过程sp_xml_preparedocument用来创建一个能被插入数据库的XML文档的内部表示,该存储过程返回一个可以访问XML文档内部表示的句柄,另一方面,系统存储过程sp_xml_removedocument可以用来删除XML文档的内部表示。

系统存储过程sp_xml_preparedocument的语法如下:

<ol><li><span><span>sp_xml_preparedocument  handleddoc  </span><span>OUTPUT</span><span>,xmltext </span></span></li></ol>
Copy after login

其中:

● handleddoc代表XML文档句柄的整数值。

● xmltext代表原始的XML文档的文本值。

系统存储过程sp_xml_removedocument的语法如下:

<ol><li><span>sp_xml_removedocument  handleddoc   </span></li></ol>
Copy after login

其中:

handleddoc代表XML文档句柄的整数值。

上述这两个存储过程都可以使用函数OPENXML,函数OPENXML的语法如下:

<ol>
<li><span>OPENXML(handleddoc,rowpattern,flagvalue)  </span></li>
<li>
<span>With</span><span> tablename </span>
</li>
</ol>
Copy after login

其中:

● handleddoc代表XML文档句柄的整数值。

● rowpattern代表来识别XML文档的节点XPath模式的可变长字符串的值。

● flagvalue代表XML数据和相关的行集之间映射的整数值。如果值为1表示要对数据库中的字段做基于属性的映射;如果值为2表示要对数据库中的字段做基于元素的映射。

● tablename代表数据库中的表名。

系统存储过程sp_xml_preparedocument读入XML文档内的文本并用MSXML解析器进行处理。处理以后,XML文档以带有元素、属性和文本的树型结构显示。OPENXML函数应用该树型结构并生成包含XML文档所有部分的行集。使用OPENXML和INSERT语句,即可以将行集中的数据插入到表中。下面通过实例进行讲解。

实例6-1  以属性的形式将XML数据插入到SQL Server数据库的表中

(1) 在SQL Server查询分析器窗口中输入以下代码:

<ol>
<li><span>USE school  </span></li>
<li>
<span>LECT * </span><span>FROM</span><span> student </span>
</li>
</ol>
Copy after login

运行后,先来查看插入之前student表中的数据,如图6-1所示。

把XML数据插入到SQL Server数据库的表中 

(2) 重新输入以下代码:

<ol>
<li><span>1  USE school  </span></li>
<li>
<span>2  </span><span>DECLARE</span><span> @doc </span><span>varchar</span><span>(1000)  </span>
</li>
<li>
<span>3  </span><span>DECLARE</span><span> @idoc </span><span>int</span><span> </span>
</li>
<li>
<span>4  </span><span>SET</span><span> @doc='<root>  </root></span>
</li>
<li><span>5           <student><span>"5"</span><span> </span><span>name</span><span>=</span><span>"cathy"</span><span> </span></student></span></li>
<li>
<span>6            sex=</span><span>"female"</span><span> age=</span><span>"21"</span><span>>  </span>
</li>
<li><span>7              </span></li>
<li><span>8            '  </span></li>
<li>
<span>9  </span><span>exec</span><span> sp_xml_preparedocument @idoc </span><span>output</span><span>,@doc  </span>
</li>
<li>
<span>10  </span><span>select</span><span> * </span><span>from</span><span> openxml(@idoc,</span><span>'/ROOT/student'</span><span>,1)  </span>
</li>
<li>
<span>11  </span><span>with</span><span>(id </span><span>int</span><span>,</span><span>name</span><span> </span><span>varchar</span><span>(40),sex </span><span>varchar</span><span>(20),  </span>
</li>
<li>
<span>12       age </span><span>int</span><span>)  </span>
</li>
<li>
<span>13  </span><span>insert</span><span> student  </span>
</li>
<li>
<span>14  </span><span>select</span><span> * </span><span>from</span><span> openxml(@idoc,</span><span>'/ROOT/student'</span><span>)  </span>
</li>
<li>
<span>15  </span><span>with</span><span> student  </span>
</li>
<li>
<span>16  </span><span>exec</span><span> sp_xml_removedocument @idoc </span>
</li>
</ol>
Copy after login

在上面代码的第4~8行创建了一个变量@doc并把XML数据存放在该变量中。然后,第9行执行系统存储过程sp_xml_preparedocument,来创建一个能把数据插入数据库的XML文档内部表示,该系统存储过程返回一个保存在变量@idoc中的句柄,可以用该句柄访问XML文档的内部表示。第10行使用一个select语句访问XML文档的内部表示,并把该文档中的所有数据显示出来。

(3) 运行程序,结果如图6-2所示。

把XML数据插入到SQL Server数据库的表中  

(4) 重新输入以下代码。

<ol>
<li><span>USE school  </span></li>
<li>
<span>SELECT</span><span> * </span><span>FROM</span><span> student </span>
</li>
</ol>
Copy after login
Copy after login

运行后,查看插入之后student表中的数据,如图6-3所示。

把XML数据插入到SQL Server数据库的表中   

通过图6-3可以清楚地看到,XML数据已经被插入到了数据库中。

实例6-2  以元素的形式将XML数据插入到SQL Server数据库的表中

接着上一个实例,我们继续进行数据的插入,不过这次是以元素的形式进行插入操作的。

首先在查询窗口中输入以下代码:

<ol>
<li><span><span>1  </span><span>DECLARE</span><span> @doc </span><span>varchar</span><span>(1000)  </span></span></li>
<li>
<span>2  </span><span>DECLARE</span><span> @idoc </span><span>int</span><span> </span>
</li>
<li>
<span>3  </span><span>SET</span><span> @doc='<root>  </root></span>
</li>
<li><span>4           <student>   </student></span></li>
<li><span>5           <id>6</id>  </span></li>
<li><span>6           <span>name</span><span>>sun</span><span>name</span><span>>  </span></span></li>
<li><span>7           <sex>male</sex>  </span></li>
<li><span>8           <age>24</age>  </span></li>
<li><span>9             </span></li>
<li><span>10          '  </span></li>
<li>
<span>11  </span><span>exec</span><span> sp_xml_preparedocument @idoc </span><span>output</span><span>,@doc  </span>
</li>
<li>
<span>12  </span><span>select</span><span> * </span><span>from</span><span> openxml(@idoc,</span><span>'/ROOT/student'</span><span>,2)  </span>
</li>
<li>
<span>13  </span><span>with</span><span>(id </span><span>int</span><span>,</span><span>name</span><span> </span><span>varchar</span><span>(40),sex </span><span>varchar</span><span>(20),  </span>
</li>
<li>
<span>14       age </span><span>int</span><span>)  </span>
</li>
<li>
<span>15  </span><span>insert</span><span> student  </span>
</li>
<li>
<span>16  </span><span>select</span><span> * </span><span>from</span><span> openxml(@idoc,</span><span>'/ROOT/student'</span><span>,2)  </span>
</li>
<li>
<span>17  </span><span>with</span><span> student  </span>
</li>
<li>
<span>18  </span><span>exec</span><span> sp_xml_removedocument @idoc </span>
</li>
</ol>
Copy after login

在上面代码的第3~10行创建了一个变量@doc,并把XML数据存放在该变量中。然后,第11行执行系统存储过程sp_xml_preparedocument,来创建一个能把数据插入数据库的XML文档的内部表示,该系统存储过程返回一个保存在变量@idoc中的句柄,可以用该句柄访问XML文档的内部表示。第12行使用一个select语句访问XML文档的内部表示,并把该文档中的所有数据显示出来了。该段代码与实例6-1中代码的唯一区别就在于变量中存放的XML数据是以元素的形式出现的,而不是属性的形式。

运行程序,结果如图6-4所示。

把XML数据插入到SQL Server数据库的表中   

重新输入以下代码:

<ol>
<li><span>USE school  </span></li>
<li>
<span>SELECT</span><span> * </span><span>FROM</span><span> student </span>
</li>
</ol>
Copy after login
Copy after login

运行后,查看插入之后student表中的数据,如图6-5所示。

把XML数据插入到SQL Server数据库的表中     

可以清楚地看到,XML数据已经被插入到了数据库中。

通过上述实例可以看到,在SQL Server 2005 中,OPENXML的功能得到了增强,它可以将XML类型数据传递到sp_xml_preparedocument存储过程中,并且可以在WITH子句中使用新的数据类型。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Slow Cellular Data Internet Speeds on iPhone: Fixes Slow Cellular Data Internet Speeds on iPhone: Fixes May 03, 2024 pm 09:01 PM

Facing lag, slow mobile data connection on iPhone? Typically, the strength of cellular internet on your phone depends on several factors such as region, cellular network type, roaming type, etc. There are some things you can do to get a faster, more reliable cellular Internet connection. Fix 1 – Force Restart iPhone Sometimes, force restarting your device just resets a lot of things, including the cellular connection. Step 1 – Just press the volume up key once and release. Next, press the Volume Down key and release it again. Step 2 – The next part of the process is to hold the button on the right side. Let the iPhone finish restarting. Enable cellular data and check network speed. Check again Fix 2 – Change data mode While 5G offers better network speeds, it works better when the signal is weaker

The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks Apr 29, 2024 pm 06:55 PM

I cry to death. The world is madly building big models. The data on the Internet is not enough. It is not enough at all. The training model looks like "The Hunger Games", and AI researchers around the world are worrying about how to feed these data voracious eaters. This problem is particularly prominent in multi-modal tasks. At a time when nothing could be done, a start-up team from the Department of Renmin University of China used its own new model to become the first in China to make "model-generated data feed itself" a reality. Moreover, it is a two-pronged approach on the understanding side and the generation side. Both sides can generate high-quality, multi-modal new data and provide data feedback to the model itself. What is a model? Awaker 1.0, a large multi-modal model that just appeared on the Zhongguancun Forum. Who is the team? Sophon engine. Founded by Gao Yizhao, a doctoral student at Renmin University’s Hillhouse School of Artificial Intelligence.

The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. May 07, 2024 pm 05:00 PM

Recently, the military circle has been overwhelmed by the news: US military fighter jets can now complete fully automatic air combat using AI. Yes, just recently, the US military’s AI fighter jet was made public for the first time and the mystery was unveiled. The full name of this fighter is the Variable Stability Simulator Test Aircraft (VISTA). It was personally flown by the Secretary of the US Air Force to simulate a one-on-one air battle. On May 2, U.S. Air Force Secretary Frank Kendall took off in an X-62AVISTA at Edwards Air Force Base. Note that during the one-hour flight, all flight actions were completed autonomously by AI! Kendall said - "For the past few decades, we have been thinking about the unlimited potential of autonomous air-to-air combat, but it has always seemed out of reach." However now,

Tesla robots work in factories, Musk: The degree of freedom of hands will reach 22 this year! Tesla robots work in factories, Musk: The degree of freedom of hands will reach 22 this year! May 06, 2024 pm 04:13 PM

The latest video of Tesla's robot Optimus is released, and it can already work in the factory. At normal speed, it sorts batteries (Tesla's 4680 batteries) like this: The official also released what it looks like at 20x speed - on a small "workstation", picking and picking and picking: This time it is released One of the highlights of the video is that Optimus completes this work in the factory, completely autonomously, without human intervention throughout the process. And from the perspective of Optimus, it can also pick up and place the crooked battery, focusing on automatic error correction: Regarding Optimus's hand, NVIDIA scientist Jim Fan gave a high evaluation: Optimus's hand is the world's five-fingered robot. One of the most dexterous. Its hands are not only tactile

Single card running Llama 70B is faster than dual card, Microsoft forced FP6 into A100 | Open source Single card running Llama 70B is faster than dual card, Microsoft forced FP6 into A100 | Open source Apr 29, 2024 pm 04:55 PM

FP8 and lower floating point quantification precision are no longer the "patent" of H100! Lao Huang wanted everyone to use INT8/INT4, and the Microsoft DeepSpeed ​​team started running FP6 on A100 without official support from NVIDIA. Test results show that the new method TC-FPx's FP6 quantization on A100 is close to or occasionally faster than INT4, and has higher accuracy than the latter. On top of this, there is also end-to-end large model support, which has been open sourced and integrated into deep learning inference frameworks such as DeepSpeed. This result also has an immediate effect on accelerating large models - under this framework, using a single card to run Llama, the throughput is 2.65 times higher than that of dual cards. one

AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! Jun 08, 2024 pm 01:00 PM

Last week, amid the internal wave of resignations and external criticism, OpenAI was plagued by internal and external troubles: - The infringement of the widow sister sparked global heated discussions - Employees signing "overlord clauses" were exposed one after another - Netizens listed Ultraman's "seven deadly sins" Rumors refuting: According to leaked information and documents obtained by Vox, OpenAI’s senior leadership, including Altman, was well aware of these equity recovery provisions and signed off on them. In addition, there is a serious and urgent issue facing OpenAI - AI safety. The recent departures of five security-related employees, including two of its most prominent employees, and the dissolution of the "Super Alignment" team have once again put OpenAI's security issues in the spotlight. Fortune magazine reported that OpenA

70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI 70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI Jun 13, 2024 pm 03:47 PM

70B model, 1000 tokens can be generated in seconds, which translates into nearly 4000 characters! The researchers fine-tuned Llama3 and introduced an acceleration algorithm. Compared with the native version, the speed is 13 times faster! Not only is it fast, its performance on code rewriting tasks even surpasses GPT-4o. This achievement comes from anysphere, the team behind the popular AI programming artifact Cursor, and OpenAI also participated in the investment. You must know that on Groq, a well-known fast inference acceleration framework, the inference speed of 70BLlama3 is only more than 300 tokens per second. With the speed of Cursor, it can be said that it achieves near-instant complete code file editing. Some people call it a good guy, if you put Curs

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

See all articles