The FOR statement executes a statement or group of statements once for each record in a record stream formed by a record selection expression. You can nest FOR statements within other FOR statements to establish relationships for outer joins. Your program can use either FOR statements or START_STREAM statements to establish record streams. You can use both methods in one program. However, you cannot use the FETCH statement to advance the pointer in a record stream established by a FOR statement. The FOR statement automatically advances to the next record.
1 – Examples
The following programs demonstrate the use of the FOR statement to create a record stream. These programs: o Declare a variable dept_code o Prompt for a value for dept_code o Start a READ_ONLY transaction o Create a record stream defined by a record selection expression that uses the value of dept_code o Display the department name for each record in that stream The C program uses the read_string function to prompt for and receive a value for dept_code. For more information on this function, see Appendix B of the "RDML Reference Manual".
1.1 – C Example
#include <stdio.h> DATABASE PERS = FILENAME "PERSONNEL"; extern void read_string (); DECLARE_VARIABLE dept_code SAME AS DEPARTMENTS.DEPARTMENT_CODE; main () { read_string ("Department Code: ",dept_code, sizeof(dept_code)); READY PERS; START_TRANSACTION READ_ONLY; FOR D IN DEPARTMENTS WITH D.DEPARTMENT_CODE = dept_code printf ("Department name = %s\n ", D.DEPARTMENT_NAME); END_FOR; COMMIT; FINISH; }
1.2 – Pascal Example
program for_in_rse (input,output); DATABASE PERS = FILENAME 'PERSONNEL'; var DECLARE_VARIABLE dept_code SAME AS DEPARTMENTS.DEPARTMENT_CODE; begin write ('Department Code: '); readln (dept_code); READY PERS; START_TRANSACTION READ_ONLY; FOR D IN DEPARTMENTS WITH D.DEPARTMENT_CODE = dept_code writeln ('Department name = ', D.DEPARTMENT_NAME); END_FOR; COMMIT; FINISH; end.
2 – Format
(B)0[m[4mFOR[m qqqqqqqqqwqq>qqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk mqq> handle-options qqqj x x lqqqqqqqqqqqqqqqqqqqqqqqqqqqqq<qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj x mqqq> rse qqwq>qqqqqqqqqqqwqqqwqq> statement qqwqq> [4mEND_FOR[m mq>on-error qqj mqq<qqqqqqqqqqqqqqqj (B)0[mhandle-options = q> ( qwqqqqqq> [4mREQUEST_HANDLE[m qqqqq> var qqqqqqqqwq> ) q> tqqqqqq> [4mTRANSACTION_HANDLE[m qqq> var qqqqqqqqu mqqqqqq> [4mREQUEST_HANDLE[m q> var q> , qqqqqqqk x lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj x mqqqq> [4mTRANSACTION_HANDLE[m q> var qqqqqqqqqqqj
2.1 – Format arguments
rse A record selection expression. A phrase that defines specific conditions that individual records must meet before Oracle Rdb includes them in a record stream. on-error The ON ERROR clause. Specifies host language statement(s) to be performed if an error occurs during the FOR loop. For more information see the entry on ON ERROR. statement Any valid RDML or host language statement to be executed within the FOR loop. Use a semicolon (;) at the end of each RDML, Pascal, or C statement. handle-options A request handle, a transaction handle, or both. REQUEST_HANDLE var The REQUEST_HANDLE keyword followed by a host language variable. A request handle identifies a compiled Oracle Rdb request. If you do not supply a request handle explicitly, RDML associates a unique request handle for the compiled request. TRANSACTION_HANDLE The TRANSACTION_HANDLE keyword followed by var a host language variable. A transaction handle identifies a transaction. If you do not supply a transaction handle explicitly, RDML uses the default transaction handle.