如何在 SQL 中从 XML 中选择值
我试图从 SQL 中的某些 XML 中获取值,但在最后部分中遇到了困难,我怎样才能从中获取值呢?
DECLARE @Info XML
SET @Info=N'<?xml version="1.0" encoding="utf-16"?>
<ArrayOfDriveData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<DriveData>
<DriveName>C:\</DriveName>
<DriveSpace>429223288832</DriveSpace>
</DriveData>
<DriveData>
<DriveName>I:\</DriveName>
<DriveSpace>228759863296</DriveSpace>
</DriveData>
</ArrayOfDriveData>'
-- Declare a handle for the xml document
DECLARE @idoc INT
-- Prepare the xml
EXEC sp_xml_preparedocument @idoc OUTPUT, @Info
SELECT
*
FROM
OPENXML (@idoc, '/ArrayOfDriveData/DriveData/,3)
我得到的结果是:
id | parentid | nodetype | localname| namespaceuri | datatype | prev | text
4 0 1 DriveData NULL NULL NULL NULL NULL
5 4 1 DriveName NULL NULL NULL NULL NULL
10 5 3 #text NULL NULL NULL NULL C:\
6 4 1 DriveSpace NULL NULL NULL 5 NULL
11 6 3 #text NULL NULL NULL NULL 429223288832
7 0 1 DriveData NULL NULL NULL 4 NULL
8 7 1 DriveName NULL NULL NULL NULL NULL
12 8 3 #text NULL NULL NULL NULL I:\
9 7 1 DriveSpace NULL NULL NULL 8 NULL
13 9 3 #text NULL NULL NULL NULL 228759863296
看来我需要的唯一列是 localname 和 Text,理想情况下我希望结果看起来像:
Drive | Space
C:\ 123456
这可能之前已经被问过,但我看不到任何人回答如何做到这一点(关于如何做的大量数据)从表中获取 XML,关于如何从 XML 获取数据到表中的内容并没有太多介绍),而且我在这方面的经验有点缺乏,所以我一直在思考如何完成这件事。
谢谢
Im trying to get the values from some XML in SQL but struggling with the final part, how can I get the values from this?
DECLARE @Info XML
SET @Info=N'<?xml version="1.0" encoding="utf-16"?>
<ArrayOfDriveData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<DriveData>
<DriveName>C:\</DriveName>
<DriveSpace>429223288832</DriveSpace>
</DriveData>
<DriveData>
<DriveName>I:\</DriveName>
<DriveSpace>228759863296</DriveSpace>
</DriveData>
</ArrayOfDriveData>'
-- Declare a handle for the xml document
DECLARE @idoc INT
-- Prepare the xml
EXEC sp_xml_preparedocument @idoc OUTPUT, @Info
SELECT
*
FROM
OPENXML (@idoc, '/ArrayOfDriveData/DriveData/,3)
The results I get are:
id | parentid | nodetype | localname| namespaceuri | datatype | prev | text
4 0 1 DriveData NULL NULL NULL NULL NULL
5 4 1 DriveName NULL NULL NULL NULL NULL
10 5 3 #text NULL NULL NULL NULL C:\
6 4 1 DriveSpace NULL NULL NULL 5 NULL
11 6 3 #text NULL NULL NULL NULL 429223288832
7 0 1 DriveData NULL NULL NULL 4 NULL
8 7 1 DriveName NULL NULL NULL NULL NULL
12 8 3 #text NULL NULL NULL NULL I:\
9 7 1 DriveSpace NULL NULL NULL 8 NULL
13 9 3 #text NULL NULL NULL NULL 228759863296
It seems the only columns I need are localname and Text, Ideall I would like the result to look like:
Drive | Space
C:\ 123456
This has probably been asked before but I couldnt see anyone answering how to do it (lots of data on how to get XML from a table, not much on how to get data into a table from XML) and my experience in this area is a bit lacking so i'm stuck on how to finish this off.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需简单查询即可。
Just plain query it.
如果您想坚持使用 openxml
,当然,可以将 DriveSpace 放入 varchar 中,但您可以将其更改为您需要的任何数据类型。
我还想指出,GSerg 下面发布的内容通常被认为是目前更正确的方法。 OPENXML 是一种较旧的方法。
If you want to stick with openxml its
That does, of course, put the DriveSpace into a varchar, but you can change that to whatever datatype you need.
I do also want to point out that what GSerg posted below is generally considered the more correct way to do it these days. OPENXML is an older method.