Re: select from <variable>
Posted in 1991
> I have a problem.
>
> In conventional way. table name is fixed:
>
> select xx,yy,zz
> from table
> where .......>
> What we want to do is to use string variable as table name.
> We want to change table name dynamically.
> Informix 4GL or ESQL/C does not have such a facility?
>
> Thank you.
>
> Teruki Niki
> Tokyo Research Lab.
> Matsushita Electric Industrial Co,. Ltd.
>
The same techniques apply to ESQL/C, but in 4GL we do this:
define qrybuf char(128), { make this sufficiently big }
tabnamebuf char(64) { and this, too }
let tabnamebuf = gettabname() { or some other technique }
let qrybuf = "select xx, yy, zz from ", tabnamebuf clipped,
" where ..."
prepare qryprep from qrybuf
declare qrycurs from qryprep
And off you go. This approach does, however, require that you pay
attention to the length of all your strings and character arrays.
And don't forget the CLIPPED on any variables you use as substrings.
This is handy for putting column names, table names, ORDER BY clauses,
etc. on the command line or in a control table or just about anywhere.
It also works very well in conjunction with the CONSTRUCT statement to
get the WHERE clause. Since it really doesn't matter *how* you assemble
the string prior to PREPAREing it, you've got a tremendous amount of
flexibility.
Enjoy,
John Ackley <john@equi.com>