-
4
- #1
Mike Lewis
Programmer
In thread184-1805753, we have been trying to solve a problem in Foxpro 2.6, and we discussed the possibility of using certain commands which are now flagged as compatibility only. That led to the question of whether such commands still function correctly in VFP 9.
The commands in question were JOIN and UPDATE. Don't confuse these with the SQL equivalents. They are quite different.
I have now tested both commands in VFP 9. First JOIN.
In Short, JOIN looks at each record in the table in the current work area (Employees); for each one, it looks for a matching record in a second table (Salaries); it then writes a record to a new table (Personnel) which contains all the field from both tables. The FOR clause defines the matching field.
So, in this example, Personnel will look like this:
This seems to work as expected.
There are a couple of optional extras:
You can add a FIELDS clause to define a list of the fields that are to be included in the new table. These can be fields from either existing table, but if they are from the WITH table (Salaries), they must be qualified with the alias, even if the name is unique between the tables.
You can extend the FOR clause to include other conditions, which can be used to filter the result. For example, the following version will result in a table that only contains records for Ricky, Fred and Ethel:
OK, now the UPDATE command.
In this case, we are updating the table in the current work area (Employees) rather than creating a new table. The FROM clause specifies a table (Salaries) from which we obtain the new values. The ON clause specifies a linking field (ID). This field must be present in both tables, and must have the same name in both. You cannot use an expression here; only a single field.
Here is the result:
So, the same end-product as with JOIN, but in this case all the data is in the original table.
By default, UPDATE expects there to be an index on the linking field in both tables. If you add the keyword RANDOM, then you don't need an index on the FROM table. Presumably, this is only for purposes of efficiency; it does not affect the result. Whether it actually makes a difference to performance in VFP is something that I haven't tested.
You will notice that, in my UPDATE example, the ID fields are character fields. I originally used integers for the ID, but this produced an error message: "The key field used to UPDATE ON must be of type Character, Date or Numeric". In other words, the command doesn't recognise an integer data type as numeric, which is interesting.
What's also interesting is that that error message, which relates specifically this version of UPDATE, is listed in the Help for VFP 9 (error 1145),although the Help for the actual command simply says it is for backward compatibility.
One final point. Intellisense doesn't seem to recognise these two commands.
Of course, all of the above is completely academic for anyone using Visual Foxpro, as we now have better ways of doing things. But the information might be useful for users of pre-Visual versions - not to mention a bit of nostalgia for the rest of us.
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads
The commands in question were JOIN and UPDATE. Don't confuse these with the SQL equivalents. They are quite different.
I have now tested both commands in VFP 9. First JOIN.
Code:
CREATE CURSOR Employees (ID I, Emp_Name C(10))
INSERT INTO Employees (ID, Emp_Name) VALUES (1, "Lucy")
INSERT INTO Employees (ID, Emp_Name) VALUES (2, "Ricky")
INSERT INTO Employees (ID, Emp_Name) VALUES (3, "Fred")
INSERT INTO Employees (ID, Emp_Name) VALUES (4, "Ethel")
CREATE CURSOR Salaries (ID I, Salary Y)
INSERT INTO Salaries (ID, Salary) VALUES (1, 1500)
INSERT INTO Salaries (ID, Salary) VALUES (2, 1600)
INSERT INTO Salaries (ID, Salary) VALUES (3, 1750)
INSERT INTO Salaries (ID, Salary) VALUES (4, 1800)
SELECT Employees
JOIN WITH Salaries TO Personnel FOR Employees.ID = Salaries.ID
In Short, JOIN looks at each record in the table in the current work area (Employees); for each one, it looks for a matching record in a second table (Salaries); it then writes a record to a new table (Personnel) which contains all the field from both tables. The FOR clause defines the matching field.
So, in this example, Personnel will look like this:
Code:
ID Emp_Name Salary
== ========= ======
1 Lucy 1500
2 Ricky 1600
3 Fred 1750
4 Ethel 1800
This seems to work as expected.
There are a couple of optional extras:
You can add a FIELDS clause to define a list of the fields that are to be included in the new table. These can be fields from either existing table, but if they are from the WITH table (Salaries), they must be qualified with the alias, even if the name is unique between the tables.
You can extend the FOR clause to include other conditions, which can be used to filter the result. For example, the following version will result in a table that only contains records for Ricky, Fred and Ethel:
Code:
JOIN WITH Salaries TO Personnel FOR Employees.ID = Salaries.ID AND Salaries.Salary > 1500
OK, now the UPDATE command.
Code:
CREATE CURSOR Employees (ID C(1), Emp_Name C(10), Salary Y)
INDEX ON ID TAG ID
INSERT INTO Employees (ID, Emp_Name) VALUES ("1", "Lucy")
INSERT INTO Employees (ID, Emp_Name) VALUES ("2", "Ricky")
INSERT INTO Employees (ID, Emp_Name) VALUES ("3", "Fred")
INSERT INTO Employees (ID, Emp_Name) VALUES ("4", "Ethel")
CREATE CURSOR Salaries (ID C(1), Salary Y)
INDEX ON ID TAG ID
INSERT INTO Salaries (ID, Salary) VALUES ("1", 1500)
INSERT INTO Salaries (ID, Salary) VALUES ("2", 1600)
INSERT INTO Salaries (ID, Salary) VALUES ("3", 1750)
INSERT INTO Salaries (ID, Salary) VALUES ("4", 1800)
SELECT Employees
UPDATE ON ID FROM Salaries REPLACE Employees.Salary with Salaries.Salary
In this case, we are updating the table in the current work area (Employees) rather than creating a new table. The FROM clause specifies a table (Salaries) from which we obtain the new values. The ON clause specifies a linking field (ID). This field must be present in both tables, and must have the same name in both. You cannot use an expression here; only a single field.
Here is the result:
Code:
ID Emp_Name Salary
== ========= ======
1 Lucy 1500
2 Ricky 1600
3 Fred 1750
4 Ethel 1800
So, the same end-product as with JOIN, but in this case all the data is in the original table.
By default, UPDATE expects there to be an index on the linking field in both tables. If you add the keyword RANDOM, then you don't need an index on the FROM table. Presumably, this is only for purposes of efficiency; it does not affect the result. Whether it actually makes a difference to performance in VFP is something that I haven't tested.
You will notice that, in my UPDATE example, the ID fields are character fields. I originally used integers for the ID, but this produced an error message: "The key field used to UPDATE ON must be of type Character, Date or Numeric". In other words, the command doesn't recognise an integer data type as numeric, which is interesting.
What's also interesting is that that error message, which relates specifically this version of UPDATE, is listed in the Help for VFP 9 (error 1145),although the Help for the actual command simply says it is for backward compatibility.
One final point. Intellisense doesn't seem to recognise these two commands.
Of course, all of the above is completely academic for anyone using Visual Foxpro, as we now have better ways of doing things. But the information might be useful for users of pre-Visual versions - not to mention a bit of nostalgia for the rest of us.
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads