XML 到 SQL 的问题 - SQL Server

2022-11-22数据库问题
3

本文介绍了XML 到 SQL 的问题 - SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我试图在 sql 脚本中获取 xml 特定元素的位置,但我正在获取特定元素的所有位置的详细信息,因为我只是在寻找特定级别.

I am trying to get position of a specific element of xml in sql script , but I am getting details for all the positions of a specific element , where as I am looking for a particular level only.

这是代码:

declare @xmlVar xml ='
<A specVersion="2.09">
  <B id="1" type="Regular">
    <C>
      <D>
        <E actioncode="A" date="06/13/2018 09:20" />
        <E  actioncode="B" date="06/13/2018 09:20" />
      </D>
      <D>
        <E actioncode="C" date="06/13/2018 09:20" />
      </D>
    </C>
  </B>

  <B id="2" type="Regular">
    <C>
      <D>
        <E  actioncode="D" date="06/13/2018 09:20" />
      </D>
    </C>
    <F>
        <D>
            <E  actioncode="F" date="06/13/2018 09:20" />
        </D>
    </F>
  </B>
</A>' ;

WITH Tally(Nmbr) AS
(
    SELECT TOP (SELECT @xmlVar.value(N'count(//D)','int'))
           ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) 
    FROM master..spt_values --just a pre-filled table with many rows
)
SELECT Nmbr
      ,e.value(N'@actioncode[1]','nvarchar(max)') AS Employee
FROM Tally
OUTER APPLY(SELECT  @xmlVar.query(N'//D').query(N'/D[sql:column("Nmbr")]')) AS B(ds)
OUTER APPLY ds.nodes(N'D/E') AS C(e);

以上查询的结果:

1   A
1   B
2   C
3   D
4   F

预期输出:

1 A
1 B
2 C
3 D

我正在寻找路径 A->B->C->D 中所有 D 的位置,而不是其他路径中的位置.

I am looking for positions of all D which comes in path A->B->C->D not the one's which come in other path.

推荐答案

好的,对于您之前的问题,这似乎是一个相当容易的更改.您可以使用相同的逻辑,但将其缩减为您想要查看的路径:

Okay, this seems to be a rather easy change to your previous question. You can use the same logic, but reduce it to the path you want to see:

WITH Tally(Nmbr) AS
(
    SELECT TOP (SELECT @xmlVar.value(N'count(/A/B/C/D)','int'))
           ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) 
    FROM master..spt_values --just a pre-filled table with many rows
)
SELECT Nmbr
      ,e.value(N'@actioncode[1]','nvarchar(max)') AS Employee
FROM Tally
OUTER APPLY(SELECT  @xmlVar.query(N'/A/B/C/D').query(N'/D[sql:column("Nmbr")]')) AS B(ds)
OUTER APPLY ds.nodes(N'D/E') AS C(e);

更新:获取

正如您所发现的,上述方法不能向后移动到 .试试这个:

WITH AllBs AS 
(
    SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS B_position
          ,b.value('@id','int') AS B_id
          ,b.query('.') AS B_node
    FROM @xmlVar.nodes('/A/B') AS A(b)
)
,AllDs As
(
    SELECT AllBs.*
          ,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS D_position
          ,d.query('.') AS D_node 
    FROM AllBs 
    OUTER APPLY B_node.nodes('B/C/D') AS A(d)
)
SELECT AllDs.*
      ,e.value(N'@actioncode[1]','nvarchar(max)') AS Employee
FROM AllDs
OUTER APPLY D_node.nodes('D/E') AS A(e);

正如我在第一个答案中所写的,排序顺序存在很小的风险......但是使用 tally 表 的方法无法获取第三次出现,因为 [sql:column()] 与任何父项重新编号.这意味着:第三个 是第二个 中的第一个.这就是为什么我将 .query() 放在中间......

As written in my first answer, there's a tiny risk about the sort order... But the approach with the tally table cannot fetch the 3rd occurance, because the position within [sql:column()] is re-numbered with any parent. That means: The 3rd <D> is the 1st in the 2nd <B>. That's why I placed the .query() in between...

这篇关于XML 到 SQL 的问题 - SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End
SQLServer

相关推荐

将可变参数列表传递给 SqlServer2008 存储过程的理智/快速方法
Sane/fast method to pass variable parameter lists to SqlServer2008 stored procedure(将可变参数列表传递给 SqlServer2008 存储过程的理智/快速方法)...
2023-10-26 数据库问题
1

为什么SqlServer select语句会选择匹配的行和匹配并带有尾随空格的行
Why would SqlServer select statement select rows which match and rows which match and have trailing spaces(为什么SqlServer select语句会选择匹配的行和匹配并带有尾随空格的行)...
2023-10-08 数据库问题
3

SQLSERVER 中的 ListAGG
ListAGG in SQLSERVER(SQLSERVER 中的 ListAGG)...
2023-07-18 数据库问题
7

相当于 mySQL 中的 SQLServer 函数 SCOPE_IDENTITY()?
The equivalent of SQLServer function SCOPE_IDENTITY() in mySQL?(相当于 mySQL 中的 SQLServer 函数 SCOPE_IDENTITY()?)...
2023-04-28 数据库问题
59

使用 spark sql 在 sqlserver 上执行查询
execute query on sqlserver using spark sql(使用 spark sql 在 sqlserver 上执行查询)...
2023-04-04 数据库问题
8

Debezium 如何使用 Kafka Connect 正确注册 SqlServer 连接器 - 连接被拒绝
Debezium How do I correctly register the SqlServer connector with Kafka Connect - connection refused(Debezium 如何使用 Kafka Connect 正确注册 SqlServer 连接器 - 连接被拒绝)...
2023-04-03 数据库问题
3