仅使用变量更新 table

Update table using only variables

我有一个 table EmpTable,如下所示:

如果我想更新 John 的薪水,我可以这样做:

static void UpdateSal(Args _args)
{
    EmpTable EmpTable;
    real sal=110000;
    int RowId = 1;

    ttsBegin;
    select forUpdate EmpTable where EmpTable.Id==RowId;
    EmpTable.Salary=sal;
    EmpTable.update();
    ttsCommit;


}

我需要帮助来仅使用变量来实现上述代码:

static void UpdateSal_WithStrValues(Args _args)
{

    str table = 'EmpTable'
    str field = 'Salary'
    int RowId = 1;
    real sal=110000;

    .....??
    .....??

}

更新:

此代码有效:

static void Job1(Args _args)
{
    SysDictTable dictTable = new SysDictTable(tablename2id('EmpTable'));
    Common common = dictTable.makeRecord();

    ttsbegin;
    while select forupdate common
        where common.(fieldName2id(tableName2Id("EmpTable"),'Id')) == 1
    {
        common.(fieldName2id(tableName2Id("EmpTable"),'Salary')) = 110100;
        common.update();
    }
    ttscommit;

}

但是这段代码没有:

static void Job1(Args _args)
{

    str table = 'EmpTable';
    str fieldToUpdate= 'Salary';
    str fieldToSelect= 'Id';
    int RowId = 1;
    real sal=34536;

    SysDictTable dictTable = new SysDictTable(tablename2id(table));
    Common common = dictTable.makeRecord();


    ttsbegin;
    while select forupdate common
        where common.(fieldName2id(tableName2Id(table),fieldToSelect)) == RowId
    {
        common.(fieldName2id(tableName2Id(table),fieldToUpdate)) = sal;
        common.update();
    }
    ttscommit;

}

将字符串限制为固定长度解决了问题:

下面的代码现在可以工作了:

static void Job1(Args _args)
{
    
    str 50 table = 'EmpTable';
    str 50 fieldToUpdate= 'Salary';
    str 50 fieldToSelect= 'Id';
    int RowId = 1;
    real sal=12213;
    
    SysDictTable dictTable = new SysDictTable(tablename2id(table));
    Common common = dictTable.makeRecord();
 
    
    ttsbegin;
    while select forupdate common
        where common.(fieldName2id(tableName2Id(table),fieldToSelect)) == RowId
    {
        common.(fieldName2id(tableName2Id(table),fieldToUpdate)) = sal;
        common.update();
    }
    ttscommit;

}

Martin Drab 的更好解决方案:

static void Job1(Args _args)
{

    TableName table = 'EmpTable';
    FieldName fieldToUpdate= 'Salary';
    FieldName fieldToSelect= 'Id';
    int rowId = 1;
    real sal = 6546456;
    
    SysDictTable dt = SysDictTable::newName(table);
    Common common = dt.makeRecord();
    
    ttsbegin;
    while select forUpdate common
        where common.(dt.fieldName2Id(fieldToSelect)) == rowId;
    {
        common.(dt.fieldName2Id(fieldToUpdate)) = sal;
    
        if (!common.validateWrite())
        {
            throw error("Nope");
        }
        common.update();
    }
    
    ttscommit;
}