You are here: Automating Analyzer > Auto-running Procedures

Auto-running Procedures

Analyzer allows procedures to be automatically run when a table is opened to ensure that an existing table is recreated or refreshed from the most current version of the source data. There are no restrictions on the contents of an auto-run procedure.

Note: Auto-run procedures cannot be used to issue commands that would be run AFTER a table is opened. Only commands that recreate or refresh the table before the table is opened are allowed.

By default, Analyzer will auto-run a procedure whose name is the same as the primary table being opened followed by a trailing underscore character “_”.

Note: Any project item whose name has a trailing underscore is hidden from display in the Overview. To View hidden items, select Tools/Options in the main menu, then select the Interface tab and check “Show Hidden Overview Items”.

The Auto-run procedure may be as simple as a single command such as "REFRESH tablename" where the table is based on an ODBC import or a flattened print image (report) file. Where the table data is generated from source data using a more complex process, then this process may also be implemented within the procedure.

Tip: When specifying an auto-run process within the table layout (as opposed to using a separate procedure with the same name) you MUST recreate the target table using the NOFORMAT command parameter, so that the original table layout is not overwritten which would cause the auto-run specification to be lost.

Tip: The NOFORMAT command parameter should also be used when the table layout has been augmented to contain additional computed fields so that the original table layout will not be overwritten and the computed fields will not be lost. For more information see Noformat.

Note: The Filedate() function is particularly useful for use in an auto-run procedure. It allows the date of last change of any file to be queried, thereby enabling selective refreshing of the data based on comparison with the date of the originating data file.

There are no restrictions on what can be done in an auto-run procedure to recreate or refresh a table, so you can include interactive commands and call other procedures or procedure labels.

Note: There is no test for an infinite loop, so an auto-run procedure should NEVER open the table being recreated or refreshed. To do so causes the auto-run procedure to be run repeatedly.

Example 1:

You want to refresh a table layout (gl) whose table data (gl.dat) is a flattened version of a source print image (report) file (report.txt) whenever the underlying print image file has been changed (updated). A procedure to do this would include the following command:

If filedate("report.txt") > filedate("gl.dat") refresh gl

This command will refresh the table data (GL.dat) only if the source print image file (Report.txt) is more recent than the flattened table data (GL.dat).

Example 2:

You want to recreate the current sales summary table (sales_summary) which accumulates sales amounts (sales_amt) to date by salesman (sales_code).

set safety off

open sales_ytd

classify on sales_code accumulate sales_amt to sales_summary

set safety on

Example 3:

You want to recreate the current sales summary table (sales_summary) which accumulates sales by salesman where the sales date (sales_date) falls within a specified period. This requires you to specify the date range to be used to filter the source sales table (sales_ytd). The filtered source records are then classified by salesman (sales_code) accumulating sales (sales_amt) for this period.

set safety off

open sales_ytd

accept “enter start date in yyyymmdd format:” to sdate

accept “enter end date in yyyymmdd format:” to edate

set filter sales_date>=ctod(sdate) and sales_date<=ctod(edate)

classify on sales_code accumulate sales_amt to sales_summary

set safety on

Note: The Set Safety command is used in examples 2 and 3 to temporarily turn off any system prompting when overwriting the Sales_Summary table.