Macro
cl
loop {for|as} var [type-spec]
being {the|each}{records|record}
{in|of} query-expression => result
A variable.
An SQL query-statement
An object.
loop macro has been extended with a clause for iterating over query results. This extension is available only when the SQL interface has been loaded. See Chapter 15, Common SQL. For a full description of the rest of the Common Lisploop facility, see the Common Lisp Hyperspec. loop example, on each record returned as a result of the query, bindsname, finds the salary (if any) from an associated hash-table, and for salaries greater than 20000: increments a count, accumulates the salary, and prints the details. Finally, the average salary is printed. (loop for (name) being each record in [select [ename] :from [emp]] as salary = (gethash name *salary-table*) initially (format t "~&~20A~10D" 'name 'salary) when (and salary (> salary 20000)) count salary into salaries and sum salary into total and do (format t "~&~20A~10D" name salary) else do (format t "~&~20A~10A" name "N/A") finally (format t "~2&Av Salary: ~10D" (/ total salaries)))
do-querymap-queryqueryselect