Executing dynamically created SQL statements that return a row set

    Available in

    PSQL

    Syntax

    The statement FOR EXECUTE STATEMENT is used, in a manner analogous to FOR SELECT, to loop through the result set of a dynamically executed query that returns multiple rows.

    Example

    Executing a dynamically constructed SELECT query that returns a data set:

    1. Q_FIELD_NAME VARCHAR(100),
    2. Q_TABLE_NAME VARCHAR(100)
    3. ) RETURNS(
    4. LINE VARCHAR(32000)
    5. )
    6. DECLARE VARIABLE P_ONE_LINE VARCHAR(100);
    7. BEGIN
    8. LINE = '';
    9. EXECUTE STATEMENT
    10. 'SELECT T1.' || :Q_FIELD_NAME ||
    11. INTO :P_ONE_LINE
    12. DO
    13. IF (:P_ONE_LINE IS NOT NULL) THEN
    14. LINE = :LINE || :P_ONE_LINE || ' ';
    15. SUSPEND;
    16. END

    See also