2012-10-08 51 views
0

我有一個查詢,用戶輸入一個項目編號,查詢顯示項目編號和項目描述,然後進入循環並顯示數據庫中每個項目編號的信息。在循環結束時,查詢將根據顯示的項目顯示庫存總量。顯示循環前的數據

如何在循環開始前顯示項目編號和項目描述?對於那一部分,一切都很好。

我想看產品編號:5產品描述:襯衫。我沒有看到項目編號和項目描述的任何數據。

PL/SQL查詢:

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; 
/

輸出:

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 

回答

2

就像你從內環路打開的遊標獲取數據,你必須在循環之前獲取數據能夠打印它。現在你只打開光標並關閉它,而不從中取出。注意檢查你是否能夠獲取一行或不行(就像你沒有找到c1%一樣)。