Subject Re: [ib-support] SQL add?
Author Lista de Discução Interbase
At 12:42 05/04/02 -0800, you wrote:
>Hi all,
>Why can I not execute this query? I get unknown table name error. but if I
>replace the params with the values it's ok. what am I missing?
>
>StrTable := 'ManuFact';
>NAME := 'MANNAME';
>
>begin
> with QRYT do
> begin
> Close;
> Parambyname('TABLE').asString := StrTable;
> ParamByName('NAME').asstring := NAME;
> SQL.Clear;
> SQL.Add('SELECT * FROM :TABLE');
> SQL.Add('ORDER BY :NAME');

I think it will not work !!!
AFAIK you could only run parametrized queries when the parameters are field
values. When you run parametrized queries teh statment is prepared the
execution plan is "planned" only once and the statment execute several
times only changing values from the parameters with the same execution
plan. If you change the table name or sort order the plan must be
replanned, wich is not useful for prepared statments.

I always use this aproach

var
wSQL:String;
begin
wSQL := 'Select * from %s order by %s';

MyQuery.SQL.Text := Format(wSQL, [aTableName, aOrder]);
MyQuery.Open;
end;

of course it will be useful if you change in some way the aTable and aOrder
variables. In the above example will be easier to read if I store the SQL
direct like this:

MyQuery.SQL.Text := 'Select * from ' + aTable + ' order by ' +
aOrder;

> Open;
> end;
>
>Thanks
>Daniel