如何在 SQL 中从 XML 中选择值

发布于 2024-12-16 23:36:05 字数 1745 浏览 0 评论 0原文

我试图从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌梦深 2024-12-23 23:36:05

只需简单查询即可。

select
  t.drivedata.value('DriveName[1]', 'varchar(max)'),
  t.drivedata.value('DriveSpace[1]', 'bigint')
from
  @info.nodes('/ArrayOfDriveData/DriveData') as t(drivedata);

Just plain query it.

select
  t.drivedata.value('DriveName[1]', 'varchar(max)'),
  t.drivedata.value('DriveSpace[1]', 'bigint')
from
  @info.nodes('/ArrayOfDriveData/DriveData') as t(drivedata);
挽清梦 2024-12-23 23:36:05

如果您想坚持使用 openxml

SELECT 
    DriveName, DriveSpace
FROM 
    OPENXML (@idoc, '/ArrayOfDriveData/DriveData',3)
    with (DriveName varchar(10) 'DriveName', DriveSpace varchar(20) 'DriveSpace)

,当然,可以将 DriveSpace 放入 varchar 中,但您可以将其更改为您需要的任何数据类型。

我还想指出,GSerg 下面发布的内容通常被认为是目前更正确的方法。 OPENXML 是一种较旧的方法。

If you want to stick with openxml its

SELECT 
    DriveName, DriveSpace
FROM 
    OPENXML (@idoc, '/ArrayOfDriveData/DriveData',3)
    with (DriveName varchar(10) 'DriveName', DriveSpace varchar(20) 'DriveSpace)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文