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:
Q_FIELD_NAME VARCHAR(100),
Q_TABLE_NAME VARCHAR(100)
) RETURNS(
LINE VARCHAR(32000)
)
DECLARE VARIABLE P_ONE_LINE VARCHAR(100);
BEGIN
LINE = '';
EXECUTE STATEMENT
'SELECT T1.' || :Q_FIELD_NAME ||
INTO :P_ONE_LINE
DO
IF (:P_ONE_LINE IS NOT NULL) THEN
LINE = :LINE || :P_ONE_LINE || ' ';
SUSPEND;
END
See also