Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2010
    Posts
    9

    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?

    Code:
     
    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)
    Here is a sample table

    Code:
    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.

  2. #2
    Join Date
    Aug 2003
    Location
    Where the Surf Meets the Turf @Del Mar, CA
    Posts
    7,776
    Provided Answers: 1
    Code:
    DECLARE
      v_item      VARCHAR2(20) := &v_item;
      v_cost      vm_inventory.cost&#37;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;
    >I continue to get compile errors
    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.

  3. #3
    Join Date
    Jan 2004
    Location
    Croatia, Europe
    Posts
    4,094
    Provided Answers: 4
    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •