一. 过滤时的值的形式
query在ax里是个可变的自动生成的查询类。让我们能更灵变的解决不少查询问题。
在query中,过滤的关键就是query.value(_value)中参数_value的格式,该参数的值可以是基本数据类型、表达式、值列表。下面会依次示例它们的使用方法。
//最普通的query如下代码
View Code
static void queryBasicDate(Args _args)
{
Query query;
QueryBuildDataSource queryBuildDataSource;
QueryBuildRange queryBuildRange;
QueryRun queryRun;
CustTable custTable;
;
query = new Query();
queryBuildDataSource = query.addDataSource(tablenum(CustTable));
queryBuildRange = queryBuildDataSource.addRange(fieldnum(CustTable,AccountNum));
queryBuildRange.value(queryValue("Customer2"));
queryRun = new QueryRun(query);
if(queryRun.next())
{
//相当于While select CustTable
Where custTable.AccountNum == Customer2
custTable = queryRun.get(tablenum(CustTable));
info(strfmt("%1-----%2",custTable.Name,custTable.AccountNum));
}
pause;
}
{
Query query;
QueryBuildDataSource queryBuildDataSource;
QueryBuildRange queryBuildRange;
QueryRun queryRun;
CustTable custTable;
;
query = new Query();
queryBuildDataSource = query.addDataSource(tablenum(CustTable));
queryBuildRange = queryBuildDataSource.addRange(fieldnum(CustTable,AccountNum));
queryBuildRange.value(queryValue("Customer2"));
queryRun = new QueryRun(query);
if(queryRun.next())
{
//相当于While select CustTable
Where custTable.AccountNum == Customer2
custTable = queryRun.get(tablenum(CustTable));
info(strfmt("%1-----%2",custTable.Name,custTable.AccountNum));
}
pause;
}
Query的值过滤形式各式个样,其Value形式如下:
View Code
Static void DavTest(Args _args)
{
Query query;
{
Query query;
QueryBuildDataSource qbds;
QueryBuildRange qbr;
QueryRun queryRun;
InventTable inventTable;
;
Query = new Query();
Query = new Query();
Qbds = query.addDataSource(tablenum(InventTable),’InventTable’);
……
queryRun = new QueryRun(query);
while(queryRun.next())
{
inventTable = queryRun.get(tablenum(InventTable));
print inventTable.recId;
}
}
Pause;
}
}
(1)基本数据类型:
Qbr = qbds.addRange(fieldnum(InventTable,ItemId));
Qbr.value(‘I-999’);
或
Qbr = qbds.addRange(fieldnum(InventTable,RecId));
Qbr.value(5646876135);
Qbr.value(‘I-999’);
或
Qbr = qbds.addRange(fieldnum(InventTable,RecId));
Qbr.value(5646876135);
但为了安全起见,一般将值使用queryvalue()处理后再传递给value()方法,如: qbr.value(queryvalue(‘I-999’)
(2)表达式:
Qbr = qbds.addRange(fieldnum(InventTable,ItemId));
Qbr.value(strfmt(‘(%1 == “%2”)’,fieldstr(InventTable,ItemId),queryvalue(‘I-999’));
Qbr.value(strfmt(‘(%1 == “%2”)’,fieldstr(InventTable,ItemId),queryvalue(‘I-999’));
到目前,你可能会觉得表达式不过是把简单的问题搞复杂而已,但是,我要说的是,表达式功能很强大,我们会在后面做更详细的介绍。
在使用strfmt来构造表达式的时候有几点要注意:
(a) 传递给value方法的参数最外层要用’value’的形式括起来,即用单引号括起来;
(b) 表达式中的每一个等式以及整个表达式都要使用小括号括起来,如:
‘((%1 == %2) || (%1 == %3))’
(c) 变量依次在表达式中用%n 的形式表示;
(d) 如果被用来做过滤的值是字符串型的,必须用双引号将值所对应的变量括起来,如:
Strfmt(‘(%1 == “%2”)’,fieldstr(InventTable,ItemId),queryvalue(‘I-999’));
(3)值列表:
Qbr = qbds.addRange(fieldnum(InventTable,ItemId));
Qbr.value(‘I-999,I-888,I-777’);
Qbr.value(‘I-999,I-888,I-777’);