If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > HOW TO PROCESS iN FOR LOOP?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-06-09, 01:36
laknar laknar is offline
Registered User
 
Join Date: Jul 2008
Posts: 80
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.
Reply With Quote
  #2 (permalink)  
Old 04-06-09, 02:51
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
The example of FOR statement in "SQL Reference Volume 2" may help you.

Quote:
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
Reply With Quote
  #3 (permalink)  
Old 04-06-09, 06:55
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
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
Reply With Quote
  #4 (permalink)  
Old 04-06-09, 09:29
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
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;
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On