SQL Server XML查询添加序列号

时间:2018-03-29 01:49:51

标签: sql sql-server xml qxmlquery

我有一个示例查询,它基本上循环支付并输出以下结果。我需要添加一个新的列序列号并填充序列值。我怎样才能做到这一点。

enter image description here

1235645 credit-card VISA 325.4800   1
1235645 gift-card   CD   325.4800   2
1235645 gift-card   MC   325.4800   3

**预期结果:上一列的序号:

DECLARE @XML AS XML=
N'<order xmlns="somenamspace/2006-10-31" order-no="1235645">
  <order-date>2017-07-24T20:48:57.000Z</order-date>
  <original-order-no>00000001</original-order-no>
  <customer>
    <customer-name>abcd abcd</customer-name>
    <customer-email>jjj@gmail.com</customer-email>
  </customer>
  <current-order-no>1235645</current-order-no>
  <payments>
    <payment>
      <credit-card>
        <card-type>VISA</card-type>
        <card-number>XXXX-XXXX-XXXX-1111</card-number>
        <card-holder>abcd</card-holder>
      </credit-card>
      <gift-card>
        <card-type>CD</card-type>
        <card-number>465795</card-number>
      </gift-card>
      <gift-card>
        <card-type>MC</card-type>
        <card-number>2345678</card-number>
      </gift-card>
      <amount>325.48</amount>
    </payment>
  </payments>
</order>';

WITH XMLNAMESPACES(DEFAULT N'somenamspace/2006-10-31')
    SELECT 
        @xml.value(N'(/order/@order-no)[1]',N'int') AS OrderNumber,
        p.value(N'local-name(.)',N'nvarchar(max)') AS PaymentType,
        p.value(N'(card-type/text())[1]','nvarchar(max)') AS CardType,
        p.value(N'(../amount/text())[1]','decimal(10,4)') AS Amount
    FROM 
        @xml.nodes(N'/order/payments/payment/*[local-name()!="amount"]') AS A(p)

1 个答案:

答案 0 :(得分:1)

你可能正在寻找:

if (WIN32)
  if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--stack,4194304 -fpermissive")
  elseif(MSVC)
    # add options for Visual C/C++ Compiler here
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /F 4194304")
  endif()
endif()

您可以删除WITH XMLNAMESPACES(DEFAULT N'somenamspace/2006-10-31') SELECT ROW_NUMBER() OVER(PARTITION BY p.value(N'local-name(.)',N'nvarchar(max)') ORDER BY (SELECT NULL)) AS SequenceNumber, @xml.value(N'(/order/@order-no)[1]',N'int') AS OrderNumber, p.value(N'local-name(.)',N'nvarchar(max)') AS PaymentType, p.value(N'(card-type/text())[1]','nvarchar(max)') AS CardType, p.value(N'(../amount/text())[1]','decimal(10,4)') AS Amount FROM @xml.nodes(N'/order/payments/payment/*[local-name()!="amount"]') AS A(p) 。有了这个,它将重新启动每个PARTITION BY p.value(N'local-name(.)',N'nvarchar(max)')的计数器,如果没有这个,您只需将您的付款编号从1到n。