XML查询问题


8

我正在尝试生成SQL查询,以从以下SQL XML中获取“ DATE”值:

屏幕截图

我已经尝试过类似的方法,但我认为我不了解这些概念。

select 
   xConfig.value('(/SearchjobConfig/QueryString/SearchCriteria/ExpressionSet/SimpleAttributeExpression) [1]','nvarchar(max)') 
from 
    Job 

这是XML文本:

<SearchJobConfig>
  <QueryID>1072</QueryID>
  <QueryString>
    <SearchCriteria name="Search query" >
      <ExpressionSet logicalOperator="AND">
        <SimpleAttributeExpression displayName="Date" npmPropertyId="4" searchOperation="GREATER_EQUAL" dataType="string" caseSensitive="false">2019-06-01T04:00:00</SimpleAttributeExpression>
        <SimpleAttributeExpression displayName="Date" npmPropertyId="4" searchOperation="LESS_EQUAL" dataType="string" caseSensitive="false">2019-06-13T03:59:59</SimpleAttributeExpression>
        <SimpleAttributeExpression displayName="Class" npmPropertyId="1056" searchOperation="EQUALS" dataType="int32" caseSensitive="false">65</SimpleAttributeExpression>
      </ExpressionSet>
    </SearchCriteria>
  </QueryString>
</SearchJobConfig>

预期的输出将是日期:

2019-06-01T04:00:00
2019-06-13T03:59:59

以及如何在同一行上获得结果。示例:

date_val_start date_val_end 2019-06-01T04:00:00 2019-06-13T03:59:59

我正在使用SQL Server 2012企业版。

Answers:


8

这能给您想要的东西吗?

DECLARE @x XML = '
<SearchJobConfig>
  <QueryID>1072</QueryID>
  <QueryString>
    <SearchCriteria name="Search query" >
      <ExpressionSet logicalOperator="AND">
        <SimpleAttributeExpression displayName="Date" npmPropertyId="4" searchOperation="GREATER_EQUAL" dataType="string" caseSensitive="false">2019-06-01T04:00:00</SimpleAttributeExpression>
        <SimpleAttributeExpression displayName="Date" npmPropertyId="4" searchOperation="LESS_EQUAL" dataType="string" caseSensitive="false">2019-06-13T03:59:59</SimpleAttributeExpression>
        <SimpleAttributeExpression displayName="Class" npmPropertyId="1056" searchOperation="EQUALS" dataType="int32" caseSensitive="false">65</SimpleAttributeExpression>
      </ExpressionSet>
    </SearchCriteria>
  </QueryString>
</SearchJobConfig>
'

DECLARE @Job TABLE(xConfig XML)

INSERT @Job ( xConfig )
VALUES ( @x )

SELECT j.*
       , ca.c.value('text()[1]', 'VARCHAR(30)') AS date_val
FROM @Job AS j
CROSS APPLY j.xConfig.nodes('/SearchJobConfig/QueryString/SearchCriteria/ExpressionSet/SimpleAttributeExpression') AS ca(c)
WHERE ca.c.exist('@displayName[.= "Date"]') = 1;

要将所有内容都放在一行上,只需执行以下操作:

SELECT *
FROM 
(
    SELECT STUFF(
    (SELECT N' ' + ca.c.value('text()[1]', 'NVARCHAR(MAX)')
     FROM @Job AS j
     CROSS APPLY j.xConfig.nodes('/SearchJobConfig/QueryString/SearchCriteria/ExpressionSet/SimpleAttributeExpression') AS ca(c)
     WHERE ca.c.exist('@displayName[.= "Date"]') = 1
     FOR XML PATH(N''), TYPE ).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 1, N'') 
) AS x(date_val);

2

如果你愿意,你可以撕碎使用同一行中的日期值,但不同的列nodes(),让每一个行ExpressionSet节点,然后使用属性displayName,并searchOptionvalue()获得开始和结束日期。

declare @x xml = '
<SearchJobConfig>
  <QueryID>1072</QueryID>
  <QueryString>
    <SearchCriteria name="Search query" >
      <ExpressionSet logicalOperator="AND">
        <SimpleAttributeExpression displayName="Date" npmPropertyId="4" searchOperation="GREATER_EQUAL" dataType="string" caseSensitive="false">2019-06-01T04:00:00</SimpleAttributeExpression>
        <SimpleAttributeExpression displayName="Date" npmPropertyId="4" searchOperation="LESS_EQUAL" dataType="string" caseSensitive="false">2019-06-13T03:59:59</SimpleAttributeExpression>
        <SimpleAttributeExpression displayName="Class" npmPropertyId="1056" searchOperation="EQUALS" dataType="int32" caseSensitive="false">65</SimpleAttributeExpression>
      </ExpressionSet>
    </SearchCriteria>
  </QueryString>
</SearchJobConfig>
';
declare @Job table(xConfig xml);
insert @Job (xConfig) values (@x);

select T.X.value('(SimpleAttributeExpression
                   [
                    @displayName = "Date" and 
                    @searchOperation = "GREATER_EQUAL"
                   ]/text())[1]', 'datetime') as date_val_start,
       T.X.value('(SimpleAttributeExpression
                   [
                    @displayName = "Date" and 
                    @searchOperation = "LESS_EQUAL"
                   ]/text())[1]', 'datetime') as date_val_end
from @Job as j
  cross apply j.xConfig.nodes('/SearchJobConfig/QueryString/
                                SearchCriteria/ExpressionSet') as T(X);

结果:

date_val_start            date_val_end
2019-06-01 04:00:00.000   2019-06-13 03:59:59.000

0

您说您想从XML中检索多个值,但随后指示您的XPath仅返回其中的第一个(#1):

/ SearchjobConfig / QueryString / SearchCriteria / ExpressionSet / SimpleAttributeExpression [ 1 ]
                                                                                             \___/
                                                                     Returns indexed item #1 _/

失去此索引子句,您将获得两个值,它们将按在输入XML中出现的顺序返回。

进一步阅读:https : //www.w3schools.com/xml/xpath_syntax.asp

就使它们成一行而言……[几乎可以肯定],但是仅其中两个值得付出努力吗?YMMV。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.