Search code examples
delphifiredac

Firedac Passing Column name as Parameter


In Delphi Firedac I have a very long query. I would like to reuse it just changing the name of a column via parameter like this

select f1, f2
from t1
where id = :par1
and :colimn_name = :value_name

Passing the :column_name parameter the reult query is like this:

select f1, f2
from t1
where id = 123
and 'department_name' = 'production'

with 2 '' wrapping the column name.

How can I avoid this, is there a specific way in FireDAC?

Thank you.


Solution

  • That's what FireDAC offers macros for: Substitution Variables

    select f1, f2
    from t1
    where id = :par1
    and &column_name = :value_name
    

    Setting the actual columns goes this way:

    myQuery.MacroByName('column_name').AsRaw := 'department_name';
    

    Edit: As the column name happens to be a DB identifier, using AsIdentifier instead of AsRaw should work as well. That could even be necessary when the column name can be some reserved word in the DB and has to be quoted somehow.