【问题标题】:pl/sql nested looppl/sql 嵌套循环
【发布时间】:2012-10-11 01:32:01
【问题描述】:

如何使用 items 表显示数据库中的所有记录?我当前的查询显示项目 894 的信息。我尝试使用循环,但没有运气。

我有两个表,inventory 和 itemid。其中 itemid 有物品编号和描述,库存表有物品的信息,例如尺寸、颜色、价格和手头数量。

 set serveroutput on
 DECLARE
    current_item    number(8);      
    totalvalue      number(8,2);
    description     varchar2(50);
    item_id         number(3);


    CURSOR Inventory_Info IS       
      SELECT 
         itemsize
        ,color
        ,curr_price
        ,qoh
        ,curr_price*qoh as Total
    FROM inventory
     WHERE itemid=Current_item;



BEGIN

    current_item:=894;
    totalvAlue:=0;  


    SELECT 
     itemdesc, itemid         
       INTO description, item_id
    FROM item
      WHERE itemid=current_item;

    DBMS_OUTPUT.PUT_LINE('Item ID: ' || TO_CHAR(item_id) || ' Item Description: ' || description);

    OPEN Inventory_Info;
    LOOP
            Fetch Inventory_Info INTO Inventory_rocord;
            EXIT WHEN Inventory_Info%NOTFOUND;

     DBMS_OUTPUT.PUT_LINE('Size: ' || Inventory_record.itemsize);
     DBMS_OUTPUT.PUT_LINE('Color: ' || Inventory_record.color);
     DBMS_OUTPUT.PUT_LINE('Price: ' || Inventory_record.curr_price);
     DBMS_OUTPUT.PUT_LINE('QOH: ' || Inventory_record.qoh);
     DBMS_OUTPUT.PUT_LINE('Value: ' || Inventory_record.total);

     TotalValue:=TotalValue + Inventory_record.total;

     End Loop;

     DBMS_OUTPUT.PUT_LINE('TOTAL VALUE: ' || TotalValue);
 Close Inventory_Info;

    EXCEPTION
       WHEN NO_DATA_FOUND THEN                      
       DBMS_OUTPUT.PUT_LINE('No inventory for Item No. '|| current_item);
    WHEN OTHERS THEN                             
       DBMS_OUTPUT.PUT_LINE('Error Message: '|| SQLERRM);

 END;

【问题讨论】:

    标签: oracle plsql


    【解决方案1】:

    如果我们暂时忘记了格式化,这可以使用光标 for 循环更简单地完成。

    set serveroutput ON
    DECLARE
    BEGIN
      FOR item_rec IN (SELECT itemdesc, itemid         
                         FROM item
                      ) LOOP
        DBMS_OUTPUT.PUT_LINE('Item ID: ' || TO_CHAR(item_rec.itemid) 
                                 || ' Item Description: ' || item_rec.itemdesc);                      
    
        FOR Inventory_record IN (SELECT itemsize
                                      , color
                                      , curr_price
                                      , qoh 
                                      , curr_price*qoh AS Total
                                   FROM inventory
                                  WHERE itemid = item_rec.itemid
                                ) LOOP
    
          DBMS_OUTPUT.PUT_LINE('Size: ' || Inventory_record.itemsize);
          DBMS_OUTPUT.PUT_LINE('Color: ' || Inventory_record.color);
          DBMS_OUTPUT.PUT_LINE('Price: ' || Inventory_record.curr_price);
          DBMS_OUTPUT.PUT_LINE('QOH: ' || Inventory_record.qoh);
          DBMS_OUTPUT.PUT_LINE('Value: ' || Inventory_record.total);
    
          TotalValue:= TotalValue + Inventory_record.total;
    
        END LOOP;      
      END LOOP;
    END;
    

    【讨论】:

    • 是的,这也是我的首选方式
    【解决方案2】:

    您使用单独的SELECT..INTO 来获取itemid,但您将itemid 分配给单个值并且不要更改它。

    查看您的查询,您可以将 itemid 提取移动到游标中并加入 2 个表。您将这些项目提取到inventory_record,但我在任何地方都看不到它的定义/声明。

    在这里,我声明了一个记录类型变量,其中包含您要获取的内容,打开游标,将详细信息获取到该游标中。由于没有明确的 where 条件,它将在 item 和 inventory 表之间执行内部连接并获取所有匹配连接条件的行。

    set serveroutput ON
    
    DECLARE
        TYPE inventory_rec IS RECORD (
          itemid item.itemid%TYPE,
          itemdesc item.itemdesc%TYPE,
          itemsize inventory.itemsize%TYPE,
          color inventory.color%TYPE,
          curr_price inventory.curr_price%TYPE,
          qoh inventory.qoh %TYPE,
          total inventory.curr_price%TYPE); -- record type to hold what cursor will be fetching
    
        inventory_record INVENTORY_REC;
        current_item     NUMBER(8);
        totalvalue       NUMBER(8, 2);
        description      VARCHAR2(50);
        item_id          NUMBER(3);
    
        CURSOR inventory_info IS
          SELECT itemid,
                 itemdesc,
                 itemsize,
                 color,
                 curr_price,
                 qoh,
                 curr_price * qoh AS Total
          FROM   inventory
                 join item USING (itemid);
    -- join item & inventory, so that it shows listings for all items present in inventory
    
    BEGIN
    
        OPEN inventory_info;
    
        LOOP
            FETCH inventory_info INTO inventory_record;
            EXIT WHEN inventory_info%NOTFOUND;
            dbms_output.Put_line('Current item: '
                                 || inventory_record.itemdesc);
            dbms_output.Put_line('Size: '
                                 || inventory_record.itemsize);
            dbms_output.Put_line('Color: '
                                 || inventory_record.color);
            dbms_output.Put_line('Price: '
                                 || inventory_record.curr_price);
            dbms_output.Put_line('QOH: '
                                 || inventory_record.qoh);
            dbms_output.Put_line('Value: '
                                 || inventory_record.total);
    
            totalvalue := totalvalue + inventory_record.total;
        END LOOP;
    
        dbms_output.Put_line('TOTAL VALUE: '
                             || totalvalue);
    
        CLOSE inventory_info;
    EXCEPTION
        WHEN no_data_found THEN
          dbms_output.Put_line('No inventory for Item No. '
                               || current_item);
        WHEN OTHERS THEN
          dbms_output.Put_line('Error Message: '
                               || SQLERRM);
    END; 
    

    【讨论】:

    • 这引导我走向一个好的方向。我想显示项目 ID 和描述,以及与该项目 ID 关联的所有库存信息,然后显示项目的总价值。我发布的查询就是这样做的,除了我必须输入一个 ID。我不想输入一个 id,我只想让它在执行时显示所有数据。您认为在 WHERE 子句中可以让一个光标指向另一个光标所在的行吗?
    • @user1084561 您不必在我的查询中输入 ID。它从 item 表中获取所有 itemid。您可以使用游标参数根据另一个值设置游标的 where 子句
    【解决方案3】:

    不要使用嵌套查找,使用 joins。数据库真的很擅长连接,集合操作的性能比逐行处理好很多。

    此外,在大多数情况下,您不需要声明游标和变量。使用游标循环,让 Oracle 为您完成繁重的工作。

    set serveroutput on  
    DECLARE     
        totalvalue      number(8,2);
    BEGIN      
        totalvAlue:=0;
        FOR inv_itm_rec IN (       
            SELECT    itm.itemid
                , itm.itemdesc
                , inv.itemsize         
                , inv.color         
                ,inv.curr_price         
                ,inv.qoh         
                ,inv.curr_price*inv.qoh as Total     
            FROM inventory  inv   
            JOIN item itm
                ON itm.itemid=inv.itemid
           ) 
         LOOP   
        DBMS_OUTPUT.PUT_LINE('ItemId: ' || inv_itm_rec.itemid);
        DBMS_OUTPUT.PUT_LINE('Description: ' || inv_itm_rec.itemdesc);
        DBMS_OUTPUT.PUT_LINE('Size: ' || inv_itm_rec.itemsize);
        DBMS_OUTPUT.PUT_LINE('Color: ' || inv_itm_rec.color);
        DBMS_OUTPUT.PUT_LINE('Price: ' || inv_itm_rec.curr_price);
        DBMS_OUTPUT.PUT_LINE('QOH: ' || inv_itm_rec.qoh);
        DBMS_OUTPUT.PUT_LINE('Value: ' || inv_itm_rec.total);
        TotalValue:=TotalValue + inv_itm_rec.total;
        END LOOP;
        DBMS_OUTPUT.PUT_LINE('TOTAL VALUE: ' || TotalValue);
    END;
    

    请注意,此解决方案假定每个 ITEM 确实具有匹配的 INVENTORY 记录。如果数据模型允许其他任何情况,这将是一个老式的仓库应用程序。

    【讨论】:

      【解决方案4】:

      如果它只是一份报告(似乎是)。考虑使用sql*plus 报告格式命令以您希望的方式显示查询的输出。

      这是一个例子:

      SQL> create table Items(
        2    i_num number,
        3    i_descr varchar2(101),
        4    i_size varchar2(3),
        5    i_price number,
        6    i_qoh number
        7  );
      
      Table created
      
      SQL> create table Inventories(
        2    id number,
        3    i_num number
        4  );
      
      Table created
      
      SQL> insert into items(i_num,i_size,i_price,i_qoh,i_descr)
        2    select 1, 'S', 123, 25, 'Item_1' from dual union all
        3    select 2, 'L', 424, 12, 'Item_1' from dual union all
        4    select 4, 'S', 45,  54, 'Item_4' from dual union all
        5    select 5, 'S', 78,  54, 'Item_4' from dual union all
        6    select 6, 'S', 123, 22, 'Item_5' from dual union all
        7    select 7, 'S', 127, 65, 'Item_5' from dual
        8  ;
      
      6 rows inserted
      
      SQL> commit;
      
      Commit complete
      
      SQL> insert into inventories
        2    select i_num, i_num
        3      from items;
      
      6 rows inserted
      
      SQL> commit;
      
      Commit complete
      

      现在我们的报告:

      SQL> column i_descr format a10
      SQL> column i_size  format a3
      SQL> column i_price format 99999
      SQL> column i_qoh   format 99999
      SQL> column value   format 99999
      SQL> break on i_descr skip 2
      SQL> compute sum label 'Total: ' of value on i_descr;
      
      SQL> select itm.i_descr
        2       , itm.i_size
        3       , itm.i_price
        4       , itm.i_qoh
        5       , sum(i_price*i_qoh) Value
        6   from inventories inv
        7   join items itm on (inv.i_num = itm.i_num)
        8   group by itm.i_num
        9          , itm.i_descr
       10          , itm.i_size
       11          , itm.i_price
       12          , itm.i_qoh
       13   order by i_descr, i_price;
      
      
      I_DESCR    I_S    I_PRICE  I_QOH  VALUE
      ---------- --- ---------- ------ ------
      Item_1     S          123     25   3075
                 L          424     12   5088
      **********                       ------
      Total:                             8163
      
      
      Item_4     L           45     54   2430
                 S           78     54   4212
      **********                       ------
      Total:                             6642
      
      
      Item_5     S          123     22   2706
                 L          127     65   8255
      **********                       ------
      Total:                            10961
      
      
      
      6 rows selected.  
      

      【讨论】:

        猜你喜欢
        • 2018-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-25
        • 2019-07-08
        • 2013-09-13
        • 2012-03-27
        相关资源
        最近更新 更多