Subject Re: [IBO] Problems with TIB_Query with Parameters
Author Helen Borrie
At 03:48 PM 25/08/2003 +0000, you wrote:
>Hello,
>
>I have two parametrized queries (TIB_Query components), one is
>UPDATE, and the other is SELECT.
>First one works perfectly:
>
>UPDATE
> TABLE1
>SET
> FIELD1 = :FIELD1
>WHERE
> FIELD2 = :FIELD2

Except TIB_Query is the wrong component for a DML query. Use TIB_DSQL or
IB_Cursor.


>However, the other one fails:
>
>SELECT
> *
>FROM
> TABLE2
>WHERE
> (FIELD3 like :FIELD3)

That's invalid SQL. Not just because there is no wildcard; but, to be
valid, a parameter has to substitute for a field value without changing the
structure of the query. Changing the criteria for a LIKE expression
changes the structure. You are also going to need to implement some way to
collect the position of the wildcard from the user.

In native IB, you would use an SQLWhereItem in the OnPrepareSQL event to
implement this.

So your "base" SQL property would be

SELECT * FROM TABLE2
ORDER BY FIELD4

and, when ready to apply or change the where clause, call InvalidateSQL on
the dataset.

In OnPrepareSQL, do

...
var
SomeWildConstant: string;
...
SomeWildConstant := '%' + QuotedStr(SomeConstant);
with Sender as TIB_Statement do
begin
SQLWhereitems.Clear;
SQLWhereItems.Add('FIELD3 LIKE + SomeWildConstant;
end;


>Both queries share exactly the same properties and settings, except
>the SQL text shown above.
>
>Using equal sign instead of LIKE (my first guess) doesn't help.
>Delphi gives an AV when trying to evaluate the value of ParamByName
>('FIELD3') in debug mode.

Using '=' should be fine. But Params[] is nil in an unprepared query. You
probably tried to evaluate it too early. Set your breakpoint *after*
ParamByName has been called (since ParamByName calls Prepare if the query
isn't prepared).

Helen