Setting a column on TZQuery.ParamByName

463 Views Asked by At

I need to run a SELECT statement where the column names are variable, as follows:

query.SQL.Text := 'SELECT A.:COLUMN '+
                  '  FROM A         ';

query.ParamByName('COLUMN').AsString := 'column_name';
query.Open();

But when I do this, there's an error in the SQL syntax because the component runs as:

SELECT A.'column_name'
  FROM A

Is there a way to set these parameters without the quotes so I can dynamically choose columns?

1

There are 1 best solutions below

0
Yuliya Narbut On

You can change your query as follow (create subqueries with all columns). All columns that you select have to be the same type:

select myData
from (
  select col1 as myData, 'col1' as colName from A
  union all
  select col2, 'col2' from A
  union all
  select colLast, 'colLast' from A
  ) as mTbl
where colName=:COLUMN