如何在 TClientDataSet 中使用 BIGINT (TLargeintField) 聚合字段?
我需要计算一个字段的最大值,但我在这样做时遇到了麻烦。假设我的字段名为“VALUE0”。我想使用 TClientDataSet 的聚合函数来执行此操作。我应该怎么办?
此代码仅在 SQL 表中为 BIGINT 的字段时才会失败:
function TFrmIo.GetMaxY(): Integer;
var
Max0: Integer;
FieldMax0: TAggregateField;
begin
if cds.Active then cds.Close;
FieldMax0 := TAggregateField.Create(cds);
FieldMax0.FieldName := 'MAX0';
FieldMax0.Calculated := true;
FieldMax0.ResultType := ftLargeint;
FieldMax0.FieldKind := fkAggregate;
FieldMax0.DataSet := cds;
FieldMax0.Expression := 'MAX(VALUE0)';
FieldMax0.Active := true;
cds.Open;
Max0 := Integer(FieldMax0.Value);
end;
我在“cds.Open”行上收到此异常:
Exception class EDBClient with message 'Type mismatch in expression.'
EDIT
根据注释中的要求,VALUE0 字段的类名称为 TLargeintField FieldKind 是 fkData。
编辑2
更改了问题和文本中的某些部分,因为现在我知道问题是关于 TClientDataSet 聚合函数中的 BIGINT 与 INTEGER 。
I need to calculate the Max value of a field, but I'm having troubles doing so. Let's say that my field is named 'VALUE0'. I would like to use the aggregate functions of TClientDataSet to do so. What should I do?
This code will fail only with fields that are BIGINT in my SQL table:
function TFrmIo.GetMaxY(): Integer;
var
Max0: Integer;
FieldMax0: TAggregateField;
begin
if cds.Active then cds.Close;
FieldMax0 := TAggregateField.Create(cds);
FieldMax0.FieldName := 'MAX0';
FieldMax0.Calculated := true;
FieldMax0.ResultType := ftLargeint;
FieldMax0.FieldKind := fkAggregate;
FieldMax0.DataSet := cds;
FieldMax0.Expression := 'MAX(VALUE0)';
FieldMax0.Active := true;
cds.Open;
Max0 := Integer(FieldMax0.Value);
end;
I get this exception on the "cds.Open" line:
Exception class EDBClient with message 'Type mismatch in expression.'
EDIT
As requested in the comment, the class name of VALUE0's field is TLargeintField and the FieldKind is fkData.
EDIT 2
Changed the question and some parts in the text because now I know that the problem is about BIGINT vs INTEGER in TClientDataSet aggregate functions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Sertac Akyuz 所指出的,在 Delphi 2010 及更低版本上不可能这样做。似乎在 Delphi XE 中已修复,尽管我还没有测试过。
http://qc.embarcadero.com/wc/qcmain.aspx?d=83610
As pointed out by Sertac Akyuz, it is not possible to do so on Delphi 2010 and below. Seems to be fixed in Delphi XE, although I haven't tested it.
http://qc.embarcadero.com/wc/qcmain.aspx?d=83610
作为一种解决方法,可以对
Expression
使用'MAX(VALUE0 * 1)'
或'MAX(VALUE0 + 0)'
。那么您的
ResultType
将是ftFloat
。只是在访问值时不要忘记对值进行四舍五入:Max0 := Round(FieldMax0.Value);
(浮点数有时往往有一个小尾巴:)As a workaround one could use
'MAX(VALUE0 * 1)'
or'MAX(VALUE0 + 0)'
forExpression
.Then your
ResultType
will beftFloat
. Just don't forget to round the value when you access it:Max0 := Round(FieldMax0.Value);
(floats tend to have a tiny tail sometimes :)