axapta - Update table using only variables
问题描述
I have a table EmpTable like so:
If I want to update the salary of John I can do it like so:
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;
}
I want help in implementing the above code with using only variables:
static void UpdateSal_WithStrValues(Args _args)
{
str table = 'EmpTable'
str field = 'Salary'
int RowId = 1;
real sal=110000;
.....??
.....??
}
Update:
This code works:
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;
}
But this code doesn't:
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;
}
推荐阅读
- ios - 如何将 imageView 作为源传递给 QuickLook 预览
- php - 未捕获的类型错误:ajax jquery 中的非法调用
- deep-learning - 如何选择用于我的模型的预训练权重?
- android - 像 Messenger 应用中一样的 Android 图库照片选择器
- json - 如何在python 3中比较两个嵌套的json
- python - 在 Python 中处理 minimummodbus 异常
- mongodb - SpringBoot ReactiveMongoTemplate 部分更新文档
- java - Jackson:无法从 START_OBJECT 令牌中反序列化 `java.lang.String` 的实例
- c# - 如何在 WPF/XAML 中以 NULL 作为初始值附加属性
- c# - 如何在路由控制器中传递两个参数