03-19-10, 22:15 #1Registered User
- Join Date
- Jan 2010
Unanswered: Having problems with a simple loop
This should be fairly direct but I continue to get compile errors attempting a While Loop. Should I being using a For Loop?
DECLARE v_item VARCHAR2(20) := &v_item; v_cost vm_inventory.cost%TYPE; v_expense CHAR(1) := '$'; loop_count Binary_Integer := 1000; BEGIN SELECT item, cost INTO v_item, v_cost FROM vm_inventory WHERE item = v_item; WHILE (v_cost/1000) <= v_cost LOOP v_expense := loop_count; END LOOP; dbms_output.put_line('Results: ' || loop_count); END; --If entered 'Tools', Results: $$$$...(16 $ signs)
CREATE TABLE vm_inventory (cost NUMBER(5), item VARCHAR2(20)); INSERT INTO vm_inventory VALUES (12000, 'Auto Parts'); INSERT INTO vm_inventory VALUES (10000, 'Electronics'); INSERT INTO vm_inventory VALUES (16000, 'Tools'); INSERT INTO vm_inventory VALUES (25000, 'Lawn and Garden');
Last edited by maxheadwound; 03-19-10 at 22:25.
03-19-10, 22:35 #2Registered User
Provided Answers: 1Code:
- Join Date
- Aug 2003
- Where the Surf Meets the Turf @Del Mar, CA
DECLARE v_item VARCHAR2(20) := &v_item; v_cost vm_inventory.cost%TYPE; v_expense CHAR(1) := '$'; loop_count BINARY_INTEGER := 1000; BEGIN SELECT item, cost INTO v_item,v_cost FROM vm_inventory WHERE item = v_item; WHILE (v_cost / 1000) <= v_cost LOOP v_expense := loop_count; END LOOP; dbms_output.Put_line('Results: ' ||loop_count); END;
ERROR? What Error? I don't see any error.
What/how does V_COST change to terminate the LOOP?You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
For most folks, they don't know, what they don't know.
Good judgement comes from experience. Experience comes from bad judgement.
03-20-10, 09:32 #3Lost Boy
Provided Answers: 4
- Join Date
- Jan 2004
- Croatia, Europe
Whatever you do, this loop will continue running because V_COST / 1000 will always be less than V_COST itself. So, either modify WHILE condition (which makes sense), or find a way to exit the loop when another condition is met (that would be a bad idea). Because, if you don't need the WHILE condition which is supposed to terminate the loop, you don't need the WHILE loop at all. Nor FOR loop. A simple LOOP, perhaps.
What is this code supposed to do, anyway?