Kbase 18076: When a table or field name is the same as a variable name
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  06/07/1998 |
|
When a table or field name is the same as a variable name
How does PROGRESS behave when you have a variable that has the same
name as a table or field in your database? For instance, how does
PROGRESS decide what to display when presented with this situation?
Unless the 4GL statement requires that a database table or field be
given, it will choose to work with the variable. In such situations,
if you wish PROGRESS to work with the db element instead, you must
clearly specify it using <table>.<field> or <database>.<table>
syntax.
This is best illustrated using examples.
DEF VAR customer AS CHAR FORMAT "x(20)" INIT "Hello there".
FOR EACH customer:
DISPLAY customer.
END.
In the example above, the FOR EACH uses the database "customer" table
since it would not be correct syntax to use a variable name. For the
DISPLAY statement, however, the variable "customer" is used. As a
result, the generated output is "Hello there" repeated for the same
number of times as the number of customer records in the database.
To make the DISPLAY statement work with customer records, the FOR
EACH must be modified as follows:
FOR EACH customer:
DISPLAY sports.customer.
END.
Similar requirements arise when a field has the same name as a
variable.
For example:
DEF VAR name AS CHAR FORMAT "x(20)" INIT "Hello there".
FOR EACH customer WHERE name BEGINS "S":
DISPLAY name.
END.
In this example, both the WHERE clause and the DISPLAY statement
assume that the variable "name" is intended. Since the variable is
initialized to "Hello there", the WHERE clause resolves to a "NOT"
and there is no resulting display whatsoever.
If the WHERE clause is changed to look for when name BEGINS "H", then
the WHERE clause resolves to a "YES" and the result is "Hello there"
being repeated the same number of times as there are customer
records.
Change the WHERE clause of the FOR EACH to designate the name field
of the customer table, and the resulting output will be different:
FOR EACH customer WHERE customer.name BEGINS "S":
DISPLAY name.
END.
Here, "Hello there" is repeated the same number of times as there
are customer records where the customer name begins with "S". Note
that the DISPLAY statement still assumes that you wish to display the
contents of the variable "name" rather than the database field,
despite the way "customer.name" is specified in the WHERE clause.
To make the FOR EACH display actual customer records, you must rewrite
the DISPLAY statement to use the proper specification:
FOR EACH customer WHERE customer.name BEGINS "S":
DISPLAY customer.name.
END.
In general, the practice of using variables named the same as db
tables or fields is not recommended, as the compiler might become
confused in some circumstances even when the programmer is properly
specifying with <dbname>.<table>.<field> syntax. Programs should be
carefully tested to be sure that results are as desired.