I am using OpenEdge 10.2a.
I need to change a value in multiple tables - I would like to do this in a function.
something like:
fChangeVal(Table1, FieldX, OldValue, NewValue)
fChangeVal(Table2, FieldX, OldValue, NewValue)
fChangeVal(Table3, FieldX, OldValue, NewValue)
FUNCTION fChangeVal RETURNS LOGICAL (INPUT cTable AS CHARACTER, cField AS CHARACTER, cOldVal AS CHARACTER, cNewVal AS CHARACTER):
DEFINE VARIABLE lFound AS LOGICAL NO-UNDO INIT TRUE.
FIND FIRST cTable WHERE cTable.cField = cOldVal NO-ERROR.
IF AVAILABLE cTable THEN
DO:
ASSIGN cTableName.cField = cNewVal.
lFound = TRUE.
END.
RETURN (lOk).
END FUNCTION.
As this is "cTable" and "cField" are looked at as the names of the table and field rather than the values stored in them.
My question is - How do I pass the table and field as parameters so that the function can use them?
You can't do that as a static query (in a FIND statement). You would need to do it dynamically using a dynamic buffer, and then you can accomplish what you are trying to do with things like FIND-FIRST(), and update the fields dynamically with BUFFER-FIELD(). Look at the documentation for the CREATE BUFFER statement, and then the FIND-FIRST() and BUFFER-FIELD() methods.
Or, the old school method is to pass the table and field names as parameters in the run statement to a non-compiled procedure. But, dynamic is definitely less tricky.