Anonymous tables and fields

To make procedures more generic, and thus more useful, you can use anonymous tables and fields as parameters. Anonymous means that the names of the tables or fields that will be passed to the procedure aren’t known when the procedure is written. Once the anonymous tables and fields have been passed to the procedures, they can be used in the procedure like ordinary tables and fields.

Be aware that limited error checking is performed when you’re using anonymous tables and fields. A procedure relies on the passed parameters to be correct at runtime. If non-existent table fields are referenced by the procedure, a null field address error will result. If fields with incompatible storage types are passed to the procedure, type incompatibility messages will result.

Two configurations are commonly used: an anonymous table with known fields, and an anonymous table with anonymous fields.

Anonymous table with known fields

In this case, the name of the table to be passed to the procedure is not known, but the names of the fields in the table are known. The table name is passed in by the calling script as an anonymous parameter, while the fields are referred to explicitly by name in the procedure. The basic syntax is shown in the following illustration.

[spacer]

Example

The following procedure builds a master mailing list from several different tables with known fields. The procedure has an anonymous table as an inout parameter. All of the tables that will be passed to the procedure must have fields with names Name, Address, City, State, and ZIP Code. Note that in the procedure, the table is referenced using the name declared by the inout parameter line at the top of the script. The procedure takes the fields from the anonymous table and writes them to the Mailing_List table.

[spacer]

Procedure Name

Add_to_Mailing_List


inout anonymous table source_table;

get first table source_table;
while err() <> EOF do
	Name of table Mailing_List = Name of table source_table;
	Address of table Mailing_List = Address of table source_table;
	City of table Mailing_List = City of table source_table;
	State of table Mailing_List = State of table source_table;
	'ZIP Code' of table Mailing_List = 'ZIP Code' of table source_table;
	save table Mailing_List;
	get next table source_table;
end while;

Anonymous table with anonymous fields

In this case, neither the name of the table nor the names of the fields in the table are known. Both the table and fields are passed into the procedure as anonymous parameters. The basic syntax is shown in the following illustration.

[spacer]

You can use the datatype() function to find out the data type associated with an anonymous field.


Example

The following procedure builds a master mailing list from records in an invoice table. The procedure has an anonymous table as an inout parameter and anonymous fields as in parameters. The tables that will be passed to the procedure will have address information but won’t necessarily have fields named Name, Address, City, State and ZIP Code. Note that in the procedure, the table and fields are referenced using the names declared by the parameter lines at the top of the script. The procedure uses the anonymous fields from the anonymous table to build the mailing list.

[spacer]

Procedure Name

Add_from_Invoice

Script:


inout anonymous table source_table;
in anonymous field Name_Field;
in anonymous field Address_Field;
in anonymous field City_Field;
in anonymous field State_Field;
in anonymous field ZIP_Field;

get first table source_table;
if err() <> EOF then
	while err() <> EOF do
		Name of table Mailing_List = Name_Field of table source_table;
		Address of table Mailing_List = Address_Field of table source_table;
		City of table Mailing_List = City_Field of table source_table;
		State of table Mailing_List = State_Field of table source_table;
		'ZIP Code' of table Mailing_List = ZIP_Field of table source_table;
		save table Mailing_List;
		get next table source_table;
	 end while;
end if;

Example

The following script uses the procedures in the previous two examples to build the Mailing_List table. The first two lines delete the existing Mailing_List table. Addresses are then added from the Customer_List table by passing it to the Add_to_Mailing_List procedure.

Next, addresses are added from the Invoice_Data table by passing the table and its corresponding fields as anonymous parameters to the Add_From_Invoice procedure. Note how fields from the Invoice_Data table correspond to fields in the Add_From_Invoice procedure. For example, the Customer Name field in the Invoice_Data table corresponds to the Name_Field in the Add_From_Invoice procedure.

[spacer]

Script Name

Build_Mailing_List

Script:


{Delete the existing Mailing_List table.}
open table Mailing_List, exclusive;
delete table Mailing_List;

{Add addresses from the Customer_List table. This is an anonymous table with known fields.}
call Add_to_Mailing_List, table Customer_List;

{Add addresses from the Invoice_Data file. This is an anonymous table with anonymous fields. Note how the fields from the Invoice_Data table are passed to the corresponding fields in the procedure.}
call Add_From_Invoice, table Invoice_Data, field 'Customer Name', field 'Customer Address', field City, field State, field 'Zip Code';


Documentation Feedback