Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2008
    Posts
    94

    Unanswered: HOW TO PROCESS iN FOR LOOP?

    DECLARE STRUNG VARCHAR(50);
    DECLARE SGROUP VARCHAR(50);

    SELECT PRODUCT,PRODUCTGROUP INTO STRUNG ,SGROUP FROM SCHEMA.PRODUCT WHERE STATUS='ACTIVE';


    The above will many records

    i have to process this by using for loop
    Please provide sample syntax to proceed furthur.

  2. #2
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    3,483
    The example of FOR statement in "SQL Reference Volume 2" may help you.

    Examples:

    In the following example, the for-statement is used to iterate over the entire
    employee table. For each row in the table, the SQL variable fullname is set to the
    last name of the employee, followed by a comma, the first name, a blank space,
    and the middle initial. Each value for fullname is inserted into table tnames.
    Code:
      BEGIN ATOMIC
        DECLARE fullname CHAR(40);
        FOR vl AS
          SELECT firstnme, midinit, lastname FROM employee
            DO
            SET fullname = lastname CONCAT ,
              CONCAT firstnme CONCAT   CONCAT midinit;
            INSERT INTO tnames VALUES (fullname);
        END FOR;
      END

  3. #3
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    I would like to challenge the statement "i have to process this by using for loop". Typically, that is not true and you can often write the procedural logic in a single, straight-forward SQL statement. So it would be helpful telling us what you finally want to do before you head of in the sub-optimal direction.
    Knut Stolze
    IBM DB2 Analytics Accelerator
    IBM Germany Research & Development

  4. #4
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    3,483
    I agree with Stolze.

    The example of FOR statement in "SQL Reference Volume 2"
    Code:
      BEGIN ATOMIC
        DECLARE fullname CHAR(40);
        FOR vl AS
          SELECT firstnme, midinit, lastname FROM employee
            DO
            SET fullname = lastname CONCAT ,
              CONCAT firstnme CONCAT   CONCAT midinit;
            INSERT INTO tnames VALUES (fullname);
        END FOR;
      END
    can be written in an INSERT statement.
    Code:
    INSERT INTO tnames
      SELECT lastname CONCAT ,
        CONCAT firstnme CONCAT   CONCAT midinit FROM employee;

Posting Permissions

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