Subject Re: [IBO] Adding Field OnPrepareSQL
Author Helen Borrie
At 11:03 AM 21/08/2003 +1200, you wrote:
>Based on some parameters I need to add Fieldname to my query at runtime,
>how do I do this. The sql statement is already assigned to Query
>component. I tried adding fieldname to SQL, but when I looked at
>monistor it is added after FROM. Is it possible to do this or I have to
>make the statement again?

You require a new SQL statement. The fields in a dataset are constructed
from the SELECT <fieldlist> clause of the statement. This provides the
structure of the output set. You can't use parameters to define that. It
requires a full unprepare and clear-out sequence to change it.

The second level of structure in the output set is the columns which are
named in the WHERE and ORDER BY clauses. Native IBO is designed so that
you can manipulate this level of the structure without completely clearing
out the statement object. Changing columns in either of these clauses
still causes an unprepare sequence. OnPrepareSQL is the place where you
can step in and change the WHERE or ORDER BY clauses using SQLWhereItems or
SQLOrder. (IBO also does the latter when you use the Ordering.. features
of the dataset).

At the third level - where your parameters operate - no unprepare/reprepare
is done, because parameters change merely the VALUES of the WHERE criteria,
not the structure of the output set. And params[] is not available to an
unprepared dataset. So you don't touch params[] in OnPrepareSQL.

Just remember that parameters can refer only to the values of items that
exist in a WHERE clause. Apply the values to the params before you call
Open (or First, in the case of TIB_Cursor, or Execute in the case of DML
statements). Once the statement is prepared, you can refer to "unknown"
parameterised WHERE columns via the Params[] structure, e.g. by iterating
through it with a FOR loop and testing for example "if Params[n].FieldName
= 'ThisField' then..."

Helen