How to Define a Virtual Column

Virtual columns are columns that do not actually exist in the database table, but are dynamically derived from the real columns in the table. (They correspond to computed or derived columns in some other databases.) The derivation logic is included in the OAM for the table. See What is an OAM?.

The logic to derive a Virtual Column can be defined as After Read and / or Before Write.

See Virtual Columns in a Table

This step defines a Monthly Salary virtual column for table MyEmployee, which is calculated as 1/12th of the value of Employee Salary column.

1.     If you haven't already done so, complete How to Create a Table now.

2.     Open table MyEmployee in the editor. The field xEmployeeMonthlySalary is already defined in the Repository as a field of type Decimal with a length of 15. Note that a virtual column must be first defined as a Repository field.

        If you are using the Add Multiple Columns dialog, you can define a new column as a virtual column, and a field definition will be created.

3.     Select Add Virtual Column from the Add menu on the Home ribbon:

4.     The Table Columns tab will list a new Inactive Virtual and the Details tab will open to define the Virtual Column:

a.     On the Details tab, enter Column Name as xEmployeeMonthlySalary, or use the ellipsis button to find and select it.

b.     Select checkbox Derive value when record is read. This will be a read-only virtual column.

c.     Complete the Virtual Column Derivation code as shown:

 

#xEmployeeMonthlySalary := #xEmployeeSalary / 12

5.     The Details tab should now look like this:

6.     The new Virtual Column is now listed on the Table Columns tab, under Read Virtuals:

7.     Save the table definition.

8.     Recompile the table, ensuring that the Compile Options setting is Rebuild OAMs:

 

9.     Your application can now retrieve Monthly Salary for an employee and the OAM will calculate the value and return it, just like a real column.