Board index » delphi » tquery won't accept runtime tablename as a :Params object

tquery won't accept runtime tablename as a :Params object

I have a very simple problem: I want to pass the database name that will be a
Params object to the FROM clause of the SQL property of my TQuery.
It seems to be impossible to do this, but maybe someone knows how.
This call to Open always fails:

if ParamCount > 0 then begin
                with qAnswerSelect do begin
                   DatabaseName := ParamStr(1);
                   Params[0].AsString := DatabaseName;
                   Open;
end;

Unfortunately, so does this method:

if ParamCount > 0 then begin
                with qAnswerSelect do begin
                   DatabaseName := ParamStr(1);
                   Params[0].AsString := '''' + DatabaseName + '''';
                   Open;
                end;
end;

The error message is "Invalid use of keyword: From".
Yeah, whatever. This is the "invalid" SQL property:

SELECT thisfield from :myDatabaseLocation

It obviously croaks on my using a Params object to satisfy the FROM clause.

ps: I definitely defined :myDatabaseLocation using the Parameter editor
beforehand.

Any Takers?

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading

 

Re:tquery won't accept runtime tablename as a :Params object


Hi Alex,

Quote
alex.t.silverst...@bender.com wrote:
> I have a very simple problem: I want to pass the database name that will be a
> Params object to the FROM clause of the SQL property of my TQuery.
> It seems to be impossible to do this, but maybe someone knows how.
> This call to Open always fails:

> if ParamCount > 0 then begin
>                 with qAnswerSelect do begin
>                    DatabaseName := ParamStr(1);
>                    Params[0].AsString := DatabaseName;
>                    Open;
> end;

> SELECT thisfield from :myDatabaseLocation

> It obviously croaks on my using a Params object to satisfy the FROM clause.

> ps: I definitely defined :myDatabaseLocation using the Parameter editor
> beforehand.

> Any Takers?

Yes. Some location in the SQL statement can't be params. You can pass the
tablename with:

tablename := 'myTable';
qAnswerSelect.SQL := 'select * from ' + tablename; //+ ' where thisfield =
:value';
// qAnswerSelect.ParambyName('value').asstring := 'xyz';
qAnswerSelect.execute;

Greeting from Cologne
  Chris

-------------------------------------------------------
Chris Gnther
chr...@netcologne.de or g...@ifacs.de
-------------------------------------------------------

Re:tquery won't accept runtime tablename as a :Params object


Quote
In article <6gtuup$25...@nnrp1.dejanews.com>, alex.t.silverst...@bender.com wrote:
>I have a very simple problem: I want to pass the database name that will be a
>Params object to the FROM clause of the SQL property of my TQuery.
>It seems to be impossible to do this, but maybe someone knows how.
>This call to Open always fails:

This is a restriction of the underlying database engine.  An important
purposed of parameters is to let the database engine "compile" the query and
then stick parameters in repeatedly and execute without re-parsing the SQL; it
can't do this if something as fundamental as the table name changes.

You can, however, dynamically generate the SQL:

Query1.SQL[0] = 'SELECT * FROM ' + TableName;
Query1.Open;

[* k...@kylecordes.com            |    Delphi Developers:  *]
[* Kyle Cordes Software Solutions |       Come see the     *]
[* http://www.kylecordes.com      | BDE Alternatives Guide *]

Other Threads