Pass table and field names as parameters to function?

Posted by kwesterlund on 15-May-2015 15:24

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?

 

All Replies

Posted by Fernando Souza on 15-May-2015 15:30

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.

Posted by Thomas Mercer-Hursh on 15-May-2015 15:41

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.

This thread is closed