ForeachRecord — Run a SELECT, map results to variables, and execute body.
ForeachRecord FieldNameList SelectStatement Body
Return Value
FieldNameListNames of variables to use for each field in each record.
SelectStatementA SELECT statement to run and get a result set from.
BodyWhat to execute for each record in the result set.
More Info
This command (like many others) is largely a semantic convenience. It is equivalent to first running a query and second executing a foreach loop except it does both in one step.

In a future release, we plan to improve on this functionality by parsing the SELECT statement and automatically create variables with the column names, similar to dict with.

Also note, dbcmd eval has similar functionality but will ask that you put the fields in an array. Note also, the tdbc::resultset package exists, though this author has not yet taken a close look at its functionality.

Check this page for info about how to set up Gen for using database-related commands.

For information regarding exceptions / errors, see here.

This code: (1) Creates a table and populates it, (2) Does a select and iterates over the result set, printing each record.

% RunSqlCreate my_table {id {integer primary key} desc text notes text}
% QQ [SqlInsertStatement my_table {desc 'one' notes 'uno'}]
% QQ [SqlInsertStatement my_table {desc 'two' notes 'dos'}]
% ForeachRecord {Desc Notes} {SELECT desc, notes FROM my_table} { puts "desc = $Desc, notes = $Notes" }
desc = one, notes = uno
desc = two, notes = dos