I am working with the Delphi FireDac technology in connection with a PostgreSQL database. I would like to set a floating value as DefaultExpression for a Field at Runtime. The next lines works fine:
FDMemTable1.FieldByName( 'some_fieldname').FieldKind := fkData;
FDMemTable1.FieldByName( 'some_fieldname').DefaultExpression := '5.5';
But when I try to factor this out with the next code, I get an error: Exception class EFDException with message '[FireDAC][Stan][Eval]-104. Type mismatch in expression'.
procedure SetDefaultFieldValue (
var aFDMemTable : TFDMemTable;
aFieldName : string;
aFieldValue : string;
aFieldKind : TFieldKind );
begin
aFDMemTable.FieldByName( aFieldName ).FieldKind := aFieldKind;
aFDMemTable.FieldByName( aFieldName ).DefaultExpression := QuotedStr( aFieldValue );
end;
What am I doing wrong?
Thanks all for the suggestions. The problem seems to be - as @Graig Young pointed out- the different kind of columns (Numeric or String etc). I needed the QuotedStr for the String-Fields. Otherwise I get an error:
[aFieldValue] Column or Function is not found. Add FireDAC.Stan.ExprFuncs to the uses clause (which I have)
So @Victoria, leaving out QuotedStr works indeed for the Numeric-Field, but factoring like the next lines doesn't work for String-Fields.
procedure SetDefaultFieldValue (
var aFDMemTable : TFDMemTable;
aFieldName : string;
aFieldValue : string;
aFieldKind : TFieldKind );
begin
aFDMemTable.FieldByName( aFieldName ).FieldKind := aFieldKind;
aFDMemTable.FieldByName( aFieldName ).DefaultExpression := aFieldValue;
end;
DefaultExpression expects a string-value. Trying to refine my question: Is it possible to factor-out DefaultExpression for "any" FieldType? if so, how?