I have a query that a user enters an item number, and the query displays the item number and item description, then goes in a loop and displays information for every item number in the database. At the end of the loop, the query will show you the total amount of inventory based on the items displayed.
How would I display the Item Number and Item Description before the loop begins? Everything is working fine expect for that one part.
I want to see Item Number: 5 Item Description: Shirt. I don’t see any data for Item Number and Item Description.
PL/SQL QUERY:
SET SERVEROUTPUT ON
DECLARE
CURSOR C1
IS
SELECT items.items_numbers, items_description, items_size, items_price, items_qoh, sum(items_price*items_qoh) "Value" FROM inventories
JOIN items ON inventories.items_number=items.items_numbers
WHERE items.items_numbers='&enter_items_number'
GROUP BY items.items_numbers, items_description, items_size, items_color, items_price, items_qoh
ORDER BY items_price;
totalvalue NUMBER(8,2);
test1 C1%rowtype;
BEGIN
OPEN C1;
totalvalue:=0;
DBMS_OUTPUT.PUT_LINE('items ID: ' || test1.items_number || 'items Description: ' || test1.items_description);
close C1;
open C1;
LOOP
fetch C1 into test1;
exit when c1%notfound;
DBMS_OUTPUT.PUT_LINE('Size: ' || test1.items_size);
DBMS_OUTPUT.PUT_LINE('Price: ' || test1.items_price);
DBMS_OUTPUT.PUT_LINE('QOH: ' || test1.items_qoh);
DBMS_OUTPUT.PUT_LINE('Value: ' || test1.items_qoh*test1.items_price);
totalvalue:=totalvalue+test1.items_qoh*test1.items_price;
END LOOP;
DBMS_OUTPUT.PUT_LINE('TOTAL VALUE: ' || totalvalue);
END;
/
Output:
Item Number: Item Description:
Size: S
Price: 25.00
QOH: 25
Value: 625.0
Size: L
Color: Blue
Price: 30.00
QOH: 100
Value: 3000.0
TOTAL VALUE: 3625.0
Just like you’re fetching data from the open cursor within the loop, you’ll have to fetch data before the loop to be able to print it. Right now you’re only opening the cursor and closing it thereafter, without fetching from it. Be aware to check whether you were able to fetch a row or not (like you did with c1%notfound).