Search code examples
openedgeprogress-4glprogress-db

how to insert the field values in the connected openedge database when table and column names are given


I've retrieved the field names of a table by giving the table name directly (INS_TEST is the name of my table). I used columns _Field-Name, _Data-Type of the _Field system table and retrieved the field names and their data types.

I want to use the retrieved field names and insert field values into those fields.

FOR EACH _File WHERE _File-Name = "INS_TEST":

    FOR EACH _Field WHERE _File-Recid = RECID(_File):
        DISPLAY _Field._Field-Name.
        DISPLAY _Field._Data-Type.
        ASSIGN _File._File-Name._Field._Field-Name = 1 WHEN (_Field._Data-Type EQ "INTEGER").
    END.
END.

But the ASSIGN statement gives an error. Suggestions please!


Solution

  • The following procedure takes a table name, a field name and a character value as parameters. It will update the field in question in the first record of that table with the value provided.

    Obviously you could write a more sophisticated WHERE clause and do other things to fancy it up for your specific needs.

    procedure x:
    
      define input parameter tbl as character no-undo.
      define input parameter fld as character no-undo.
      define input parameter xyz as character no-undo.
    
      define variable qh as handle no-undo. 
      define variable bh as handle no-undo.
      define variable fh as handle no-undo.
    
      create buffer bh for table tbl.
      create query qh.
      qh:set-buffers( bh ).
      qh:query-prepare( "for each " + tbl ).
      qh:query-open.
    
      do transaction:
        qh:get-first( exclusive-lock ).
        fh = bh:buffer-field( fld ).
        display fh:buffer-value.
        fh:buffer-value = xyz.
      end.
    
      delete object bh.
      delete object qh.
    
      return.
    
    end.
    
    run x ( "customer", "name", "fred" ).
    
    /* prove that we really did change it...
     */
    
    find first customer no-lock.
    
    display name.