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 – 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; }
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.