T-SQL:跨应用; 上线的项目太多迭代(T-SQL: cross apply; too man

2019-09-24 00:16发布

我几乎得到了我想要的粉碎了一些严重的Xml后 - 但看结果后,我看到,在解析的一个部分,我不能轻易化解通过所有的线细节迭代的这种模式每个subheaders--因此,而不是写了一个总让我们说3个记录所有行项目的,我写了三个订单项,每个潜艇 - 这些让我们说我有两个。 我风与一共有6! :-(我已经蒸出图案作为在下面的代码的通用报头/子报头/详细关系模型。

    DECLARE @MetroXML xml
    SET @MetroXML =
    '<Header>
       <col1>Conoco</col1>
       <col2>ORD-1111</col2>
       <SubHeaders>
         <SubHeader>
          <col1>Dallas</col1>
          <col2>BOL-2213</col2>
         <Details>
          <Detail>
            <col1>Diesel</col1>
            <col2>7600.00</col2>
          </Detail>
         </Details>
        </SubHeader>
        </SubHeaders>
        <SubHeaders>
         <SubHeader>
          <col1>Fort Worth</col1>
          <col2>BOL-2216</col2>
         <Details>
          <Detail>
            <col1>Ethanol</col1>
            <col2>1852.00</col2>
          </Detail>
          <Detail>
            <col1>Unleaded</col1>
            <col2>900.00</col2>
          </Detail>
         </Details>
       </SubHeader>
      </SubHeaders>
     </Header>';


    INSERT INTO [scratch].GenericHeader
     SELECT T.c.value('col1[1]','varchar(10)') AS 'col1',
            T.c.value('col2[1]','varchar(10)') AS 'col2'
       FROM @MetroXML.nodes('/Header') T(c);


    INSERT [scratch].GenericSubHeader
     (id,col1,col2)
    SELECT 
      h.id,
      n.x.value('col1[1]','varchar(10)') AS 'col1',
      n.x.value('col2[1]','varchar(10)') AS 'col2'
     FROM [scratch].GenericHeader h
      CROSS APPLY @MetroXML.nodes('/Header/SubHeaders/SubHeader') n(x);


     INSERT [scratch].GenericDetail
     (id,subid,col1,col2)
      SELECT 
       s.id,
       s.subid,
       n.x.value('col1[1]','varchar(10)') AS 'col1',
       n.x.value('col2[1]','varchar(10)') AS 'col2'
     FROM [scratch].GenericSubHeader s
      CROSS APPLY @MetroXML.nodes('/Header/SubHeaders/SubHeader/Details/Detail') as n(x);


     select * from [scratch].GenericHeader
      where id = 24;

     select * from [scratch].GenericSubHeader
      where id = 24;

     select * from [scratch].GenericDetail
      where id = 24;

注:ID,子ID,detid被定义为INT IDENTITY(1,1)的结果

我能得到什么:

id|subid|detid|col1     |col2
--------------------------------
24|44   |22   |Diesel   |7600.00
24|44   |23   |Ethanol  |1852.00
24|44   |24   |Unleaded |900.00
24|48   |25   |Diesel   |7600.00
24|48   |26   |Ethanol  |1852.00
24|48   |27   |Unleaded |900.00

我希望得到什么:

id|subid|detid|col1     |col2
--------------------------------
24|44   |22   |Diesel   |7600.00
24|48   |23   |Ethanol  |1852.00
24|48   |24   |Unleaded |900.00

Answer 1:

你重复行的原因是因为你使用的交叉应用针对各行的整个XML GenericSubHeader 。 你必须想出一个办法已生成地图IDGenericHeader和生成的subidGenericSubHeader到XML的相关部分。

如果你是SQL Server 2008上或更高版本,你可以与输出合并 ,以创建包含生成的ID和所属的XML子节点表变量。

declare @GH table
(
  id int,
  sub xml
);

merge scratch.GenericHeader as T
using 
  (
    select T.c.value('col1[1]','varchar(10)'),
           T.c.value('col2[1]','varchar(10)'),
           T.c.query('SubHeaders')
    from @MetroXML.nodes('/Header') T(c)
  ) as S(col1, col2, sub)
on 0 = 1
when not matched then
  insert (col1, col2) values(S.col1, S.col2)
output inserted.id, S.sub into @GH;  

declare @GSH table
(
  id int,
  subid int,
  det xml
);

merge scratch.GenericSubHeader as T
using
  (
    select h.id,
           n.x.value('col1[1]','varchar(10)'),
           n.x.value('col2[1]','varchar(10)'),
           n.x.query('Details')
    from @GH as h
      cross apply h.sub.nodes('/SubHeaders/SubHeader') n(x)
  ) as S(id, col1, col2, det)
on 0 = 1
when not matched then
  insert (id, col1, col2) values (S.id, S.col1, S.col2)
output inserted.id, inserted.subid, S.det into @GSH;

insert into scratch.GenericDetail(id, subid, col1, col2)
select s.id,
       s.subid,
       n.x.value('col1[1]','varchar(10)') AS 'col1',
       n.x.value('col2[1]','varchar(10)') AS 'col2'
from @GSH as s
  cross apply s.det.nodes('/Details/Detail') as n(x);


文章来源: T-SQL: cross apply; too many iterations on line items