显示用户定义的项目ID的项目和库存信息

时间:2014-04-18 00:57:34

标签: plsql cursor implicit explicit ampersand

因此,我的标题状态的任务是显示用户定义的项目ID中的项目和库存信息。我必须编写一个返回Item_desc的单行select语句,然后将其显示为标题行。假设用户通过使用"&"输入了项目ID为2 (&符号)变量,格式化输出如下:这适用于PLSQL btw

Item ID: 2 Item Description: 3-Season Tent

我还必须创建一个显式或隐式游标,返回并显示每个库存项目的大小,颜色,当前价格,库存数量和总价值(价格*库存数量)。到目前为止,我有这个,但我不知道如果我朝着正确的方向前进。

--CORRECT CODE IS LISTED BELOW WITH NEXT PROBLEM

更新!!:好的,我把代码工作但是下一部分,我要创造一个变量,计算所有清单项目的总价值,然后在所有行进行处理后再显示....应该看起来像这样:

TOTAL VALUE: --WHATEVER TOTAL VALUE ITS SUPPOSED TO BE FOR THE PARTICULAR ITEM

我真的很开心,但这还不能正确计算...

declare
item_descript   item.item_desc%type;
iItem_id    item.item_id%type;
inItemid    inventory.item_id%type;
invcolor    inventory.color%type;
invsize     inventory.inv_size%type;
invprice    inventory.inv_price%type;
invqoh      inventory.inv_qoh%type;
value       number;
newval      number;
totalval    number;

cursor c1 IS
SELECT item.item_id, item.item_desc, inventory.item_id, inventory.color, inventory.inv_size, inventory.inv_price, inventory.inv_qoh
from item join inventory on item.item_id=inventory.item_id
where item.item_id=&item_id;




begin


open c1;

    fetch c1 into iItem_id, item_descript, inItemid, invcolor, invsize, invprice, invqoh;
    --exit when c1%notfound;

    DBMS_OUTPUT.PUT_LINE('Item ID: '||iItem_id||' Item Description: '||item_descript);


close c1;


open c1;
loop

    fetch c1 into iItem_id, item_descript, inItemid, invcolor, invsize, invprice, invqoh;



        exit when c1%notfound;

        value := (invprice*invqoh);

        newval := value; -- THIS IS WHERE I'M NOT SURE AS HOW TO CALCULATE FOR TOTAL VALUE???

        totalval := value + newval; --NOT SURE ?????



    DBMS_OUTPUT.PUT_LINE('================');
    DBMS_OUTPUT.PUT_LINE('Size: '||invsize);
    DBMS_OUTPUT.PUT_LINE('Color: '||invcolor);
    DBMS_OUTPUT.PUT_LINE('Price: '||invprice);
    DBMS_OUTPUT.PUT_LINE('QOH: '||invqoh);
    DBMS_OUTPUT.PUT_LINE('Value: '||value);

end loop;




    DBMS_OUTPUT.PUT_LINE('TOTAL VALUE: '||totalval); --THIS OUTPUT SHOULD BE CORRECT IF ONLY IT WOULD CALCULATE CORRECTLY




close c1;



commit;
end;
/

3 个答案:

答案 0 :(得分:0)

Hi I think the problem is with the select query in the cursor. Please try this and let me know for any issues.


DECLARE
  item_descript item.item_desc%type;
  iItem_id item.item_id%type;
  inItemid inventory.item_id%type;
  invcolor inventory.color%type;
  invsize inventory.inv_size%type;
  invprice inventory.inv_price%type;
  invqoh inventory.inv_qoh%type;
  value NUMBER;
  CURSOR c1
  IS
    SELECT item.item_id,
      item.item_desc,
      inventory.item_id,
      inventory.color,
      inventory.inv_size,
      inventory.inv_price,
      inventory.inv_qoh
    FROM item,
      inventory
    WHERE item.item_id=inventory.item_id
    AND item_id       =
      &item_id;
BEGIN
  OPEN c1;
  LOOP
    FETCH c1
    INTO iItem_id,
      item_descript,
      inItemid,
      invcolor,
      invsize,
      invprice,
      invqoh;
    EXIT
  WHEN c1%notfound;
    IF iItem_id=inItemid THEN
      value   :=(invprice*invqoh);
      SELECT item_desc
      INTO item_descript
      FROM item
      ,inventory
      WHERE item.item_id  =inventory.item_id
      AND item.item_id  =iItem_id
      AND inventory.item_id=inItemid;
    END IF;
  END LOOP;
  DBMS_OUTPUT.PUT_LINE('Item ID: '||iItem_id||'Item Description: '||item_descript);
  DBMS_OUTPUT.PUT_LINE('================');
  DBMS_OUTPUT.PUT_LINE('Size: '||invsize);
  DMBS_OUTPUT.PUT_LINE('Color: '||invcolor);
  DBMS_OUTPUT.PUT_LINE('Price: '||invprice);
  DBMS_OUTPUT.PUT_LINE('QOH: '||invqoh);
  DBMS_OUTPUT.PUT_LINE('Value: '||value);
END;

答案 1 :(得分:0)

declare
item_descript   item.item_desc%type;
iItem_id    item.item_id%type;
inItemid    inventory.item_id%type;
inv   color inventory.color%type;
invsize     inventory.inv_size%type;
invprice    inventory.inv_price%type;
invqoh      inventory.inv_qoh%type;
value       number;
--totalval  number;

cursor c1 IS
SELECT item.item_id, item.item_desc, inventory.item_id, inventory.color, inventory.inv_size, inventory.inv_price, inventory.inv_qoh
from item join inventory on item.item_id=inventory.item_id
where item.item_id=&item_id;




begin


open c1;

    fetch c1 into iItem_id, item_descript, inItemid, invcolor, invsize, invprice, invqoh;
    --exit when c1%notfound;

    DBMS_OUTPUT.PUT_LINE('Item ID: '||iItem_id||'Item Description: '||item_descript);


close c1;


open c1;
loop

    fetch c1 into iItem_id, item_descript, inItemid, invcolor, invsize, invprice, invqoh;



        exit when c1%notfound;

        value := (invprice*invqoh);









    DBMS_OUTPUT.PUT_LINE('================');
    DBMS_OUTPUT.PUT_LINE('Size: '||invsize);
    DBMS_OUTPUT.PUT_LINE('Color: '||invcolor);
    DBMS_OUTPUT.PUT_LINE('Price: '||invprice);
    DBMS_OUTPUT.PUT_LINE('QOH: '||invqoh);
    DBMS_OUTPUT.PUT_LINE('Value: '||value);

end loop;   





close c1;
commit;
end;
/

答案 2 :(得分:0)

hey can you try this and let me know for any issues.Thanks

declare
item_descript   item.item_desc%type;
iItem_id    item.item_id%type;
inItemid    inventory.item_id%type;
invcolor    inventory.color%type;
invsize     inventory.inv_size%type;
invprice    inventory.inv_price%type;
invqoh      inventory.inv_qoh%type;
value       number;
newval      number;
totalval    number;

cursor c1 IS
SELECT item.item_id, item.item_desc, inventory.item_id, inventory.color, inventory.inv_size, inventory.inv_price, inventory.inv_qoh
from item join inventory on item.item_id=inventory.item_id
where item.item_id=&item_id;




begin


open c1;

    fetch c1 into iItem_id, item_descript, inItemid, invcolor, invsize, invprice, invqoh;
    --exit when c1%notfound;

    DBMS_OUTPUT.PUT_LINE('Item ID: '||iItem_id||' Item Description: '||item_descript);


close c1;


open c1;
loop

    fetch c1 into iItem_id, item_descript, inItemid, invcolor, invsize, invprice, invqoh;



        exit when c1%notfound;

        totalval := totalval + (invprice*invqoh);

        value:= (invprice*invqoh);

       -- newval := value; -- THIS IS WHERE I'M NOT SURE AS HOW TO CALCULATE FOR TOTAL VALUE???

        --totalval := value + newval; --NOT SURE ?????



    DBMS_OUTPUT.PUT_LINE('================');
    DBMS_OUTPUT.PUT_LINE('Size: '||invsize);
    DBMS_OUTPUT.PUT_LINE('Color: '||invcolor);
    DBMS_OUTPUT.PUT_LINE('Price: '||invprice);
    DBMS_OUTPUT.PUT_LINE('QOH: '||invqoh);
    DBMS_OUTPUT.PUT_LINE('Value: '||value);

end loop;




    DBMS_OUTPUT.PUT_LINE('TOTAL VALUE: '||totalval); --THIS OUTPUT SHOULD BE CORRECT IF ONLY IT WOULD CALCULATE CORRECTLY




close c1;



commit;
end;