Вопрос

I'm trying to import an xml-file via a a stored procedure into a MSSQL 2012 server. As you can see in the xml-file it consists out of couple of nodes which I want to import into the tables:

production_orders - whereas one xml file is represented by one record in this table

operations - a production_order consists out of n operations

bom_items - an operation consists out of n consumed items

I have difficulties with the insertion into the bom_items table. I tried it with the following (part of the) procedure. My problem here is that I cannot access the operation_id - or better to say - it remains empty in the database. Is it generally possible to access the node above the ConsumedItem in this manner or is my approach wrong?

/** INSERT FOR bom_items **/
     DECLARE db_cursor CURSOR FOR  
     SELECT
            bom_id = line.value('(ItemID/ID)[1]','nvarchar(255)'),
            operation_id = line.value('(../ID)[1]','nchar(10)'),
            stock_id = line.value('(Location/ID)[1]','nvarchar(255)'),
            article_description = line.value('(Description)[1]','nvarchar(255)'),
            line_number = line.value('(LineNumber)[1]','int'),
            quantity = line.value('(Quantity)[1]','float')
            FROM @xmlData.nodes('/DataArea/ProductionOrder/ProductionOrderDetail/BillOfResources/Operations/ConsumedItem')as A(line)
    OPEN db_cursor   
    FETCH NEXT FROM db_cursor INTO @bom_id, @operation_id, @stock_id, @article_description, @line_number, @quantity

    WHILE @@FETCH_STATUS = 0
    BEGIN   
           IF EXISTS (SELECT * FROM bom_items WHERE production_order_id = @production_order_id AND line_number = @line_number AND operation_id = @operation_id)
                      UPDATE bom_items
                      SET   bom_id = @bom_id,
                            stock_id = @stock_id,
                            article_description = @article_description,
                            quantity = @quantity
                      WHERE production_order_id = @production_order_id AND line_number = @line_number

                ELSE
                      INSERT INTO bom_items (bom_id, production_order_id, stock_id, operation_id, article_description, line_number, quantity)
                      SELECT @bom_id, @production_order_id, @stock_id, @operation_id, @article_description, @line_number, @quantity
           FETCH NEXT FROM db_cursor INTO @bom_id, @stock_id, @operation_id, @article_description, @line_number, @quantity    
    END   

    CLOSE db_cursor   
    DEALLOCATE db_cursor    

XML-File:

<DataArea>
        <ProductionOrder>
          <ProductionOrderHeader type="ShopFloor">
            [...]
          </ProductionOrderHeader>
          <ProductionOrderDetail>
            <Sequence>1</Sequence>
            <Status>
              <Code listID="Production Order Status">Released</Code>
              <EffectiveDateTime>2013-11-13T10:45:51Z</EffectiveDateTime>
            </Status>
            <BillOfResources>
              <Operations>
                <ID>10</ID>
                <NextID>20</NextID>
                <Note/>
                <Status>
                  <Code listID="Production Order Status">Planned</Code>
                  <EffectiveDateTime>2013-11-13T10:45:51Z</EffectiveDateTime>
                </Status>
                <ConsumedItem>
                  <ItemID>
                    <ID>BL_BLU</ID>
                  </ItemID>
                  <Description>Gehäuse blau</Description>
                  <LineNumber>10</LineNumber>
                  <Quantity unitCode="ST">1</Quantity>
                  <BaseUOMQuantity unitCode="ST">1</BaseUOMQuantity>
                  <Location type="Warehouse">
                    <ID>W_RM</ID>
                  </Location>
                </ConsumedItem>
                <ConsumedItem>
                  <ItemID>
                    <ID>BL_INC</ID>
                  </ItemID>
                  <Description>Glühlampe</Description>
                  <LineNumber>20</LineNumber>
                  <Quantity unitCode="ST">3</Quantity>
                  <BaseUOMQuantity unitCode="ST">3</BaseUOMQuantity>
                  <Location type="Warehouse">
                    <ID>W_RM</ID>
                  </Location>
                </ConsumedItem>
              </Operations>
              <Operations>
                <ID>20</ID>
                <Note/>
                <Status>
                  <Code listID="Production Order Status">Planned</Code>
                  <EffectiveDateTime>2013-11-13T10:45:51Z</EffectiveDateTime>
                </Status>
                <OutputItem>
                  <ItemID>
                    <ID>BIKELIGHT_RAC_BLU_INC_3_MET_I</ID>
                  </ItemID>
                  <Description>Bikelight Modell Racer</Description>
                  <Location type="Warehouse">
                    <ID>W_FWB</ID>
                  </Location>
                </OutputItem>
              </Operations>
            </BillOfResources>
          </ProductionOrderDetail>
        </ProductionOrder>
      </DataArea>
Это было полезно?

Решение 2

The problem resided within the second

 FETCH NEXT FROM db_cursor INTO @bom_id, @stock_id, @operation_id, @article_description, @line_number, @quantity    

whereas the order of the cursor is bom_id, operation_id, stock_id (...) therefore the stock_id was mapped to the operation_id and vice versa.

Thanks to all readers of this question.

Другие советы

First of all, to avoid duplicating your code and possible errors like that, I suggest to use cursor structure like this:

declare db_cursor cursor for
    select ...

open db_cursor
while 1 = 1
begin
    fetch db_cursor into @variable
    if @@fetch_status <> 0
       break
    ...
end
close db_cursor
deallocate db_cursor

and second, why do you need cursor here at all? You can use just one or two statements, for example, use merge for this operation, or update + insert, like this:

merge table1 as t
using (
    select
        a.b.value('key1[1]', 'int') as key1,
        a.b.value('val1[1]', 'int') as val1,
        a.b.value('val2[1]', 'int') as val2
    from @xmlData.nodes('root/data') as a(b)
) as x on x.key1 = t.key1
when matched then
    update set
        val1 = x.val1,
        val2 = x.val2
when not matched then
    insert (key1, val1, val2)
    values (x.key1, x.val1, x.val2);

sql fiddle demo

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top