HELPLIB.HLB  —  RDML72  Statements  FETCH
    Retrieves the next record from a record stream. The FETCH
    statement is used:

    o  After a START_STREAM statement

    o  Before any other RDML statements that affect the context
       established by the START_STREAM statement

    The FETCH statement advances the pointer for a record stream to
    the next record of a relation. Unlike the FOR statement, which
    advances to the next record automatically, the FETCH statement
    allows you explicit control of the record stream. For instance,
    you might use the FETCH statement to print a report where the
    first six rows have five columns, and the seventh row only three.

1  –  Examples

    The following programs demonstrate the use of the FETCH statement
    to advance a pointer in an open stream. These programs

    o  Fetch a record from a stream specified by the DECLARE_STREAM
       statement

    o  If at least one record is found, enter a "while" loop

    o  Modify the record

    o  Fetch and modify all the records in the stream

    o  End the stream

1.1  –  C Example

    #include <stdio.h>
    #define TRUE 1
    #define FALSE 0

    DATABASE PERS = FILENAME "PERSONNEL";

    DECLARE_STREAM sal USING SH IN SALARY_HISTORY
         WITH SH.SALARY_AMOUNT LT 10000;

    int end_of_stream;

    main()
    {
    READY PERS;
    START_TRANSACTION READ_WRITE;

        START_STREAM sal;

        FETCH sal
         AT END
           end_of_stream = TRUE;
        END_FETCH;

        while (! end_of_stream)
           {
           MODIFY SH USING
             SH.SALARY_AMOUNT = SH.SALARY_AMOUNT * (1.5);
           END_MODIFY;

           FETCH sal
             AT END
               end_of_stream = TRUE;
           END_FETCH;
           }

        END_STREAM sal;

        COMMIT;
        FINISH;
    }

1.2  –  Pascal Example

    program anycond (input,output);
    DATABASE PERS = FILENAME 'PERSONNEL';

    var
    end_of_stream : boolean;

    DECLARE_STREAM sal USING SH IN SALARY_HISTORY
         WITH SH.SALARY_AMOUNT LT 10000;

    begin
        READY PERS;
        START_TRANSACTION READ_WRITE;

        START_STREAM sal;

        FETCH sal
         AT END
           end_of_stream := TRUE;
        END_FETCH;

        while not end_of_stream do
         begin
           MODIFY SH USING
             SH.SALARY_AMOUNT := SH.SALARY_AMOUNT * (1.5);
           END_MODIFY;

           FETCH sal
             AT END
               end_of_stream := TRUE;
           END_FETCH;

         end;

        END_STREAM sal;
        COMMIT;
        FINISH;
    end.

2  –  Format

  (B)0FETCH qq> stream-name qqqqqqqwqq>qqqqqqqqqqqqqwqqqqqk
                               mqq> on-error qqqj     x
  lqqqqqqqqqqqqqqqqqq<qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj
  mqwqq>qqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwqqq>
    tqq> AT END qqwqq> statement qqwq> END_FETCH  qqu
    x             mqqqqqqq<qqqqqqqqj                x
    mqq> END_FETCH qqqqqqqqqqqqqqqqqq>qqqqqqqqqqqqqqj

2.1  –  Format arguments

    stream-name            The stream from which you want to FETCH
                           the next record.

    on-error               The ON ERROR clause. Specifies host
                           language statement(s) to be performed
                           if an error occurs during the FETCH
                           operation. For more information see the
                           entry on ON ERROR.

    statement              Any valid RDML or host language statement
                           to be executed when your program reaches
                           the end of a record stream. Use a
                           semicolon (;) at the end of each RDML,
                           Pascal, or C statement.
Close Help