anant desai
20th June 2012, 07:42
I have a SELECT statement in which the
WHERE clause changes depending on entries
on the input form.Do I have to use Dynamic SQL
to do this?
JaapJD
20th June 2012, 09:21
Mostly not. Because you can also include non-table fields in where clauses. Example to include closed orders based on a flag on the form:
where tisfc001.stat < tistat.closed or
:include.closed = tcyesno.yes
anant desai
20th June 2012, 11:32
What I want to achieve is as follows:
If the user enters Days diffrence then
the where clause should be Order date inrange stdt and endt
where stdt is TODAY and endt is stdt - Days Diffrence,
else the user enters range of Order Numbers in which case
where clause becomes tdsls040.orno inrange orno.f and orno.t
where orno.f and orno.t are input fields on the form.
JaapJD
20th June 2012, 13:38
Still no need for dynamic SQL, but for performance reasons I would write 2 queries:
if diff.days = 0 then
select tdsls040.*
from tdsls040
where tdsls040.orno inrange :orno.f and :orno.t
selectdo
...
endselect
else
select tdsls040.*
from tdsls040
where tdsls040.odat inrange :endt and :stdt
selectdo
...
endselect
Note: endt and stdt seem to be in wrong order, but must be coded this way based on your description.
Other possibility but worse for performance:
select tdsls040.*
from tdsls040
where (:diff.days = 0 and tdsls040.orno inrange :orno.f and :orno.t)
or (:diff.days <> 0 and tdsls040.odat inrange :endt and :stdt)
selectdo
...
endselect
The 3rd possibility is indeed dynamic SQL:
query = "select tdsls040.* from tdsls040 where "
if diff.days = 0 then
query = query & "tdsls040.orno inrange " & str$(orno.f) & " and " & srtr$(orno.t)
else
query = query & "tdsls040.odat inrange " & str$(endt) & " and " & str$(stdt)
endif
sql.id = sql.parse(query)
ret = sql.exec(sql.id)
while sql.fetch(sql.id) = 0
...
endwhile
Note: code is not tested by me...
anant desai
20th June 2012, 14:02
Thank you once again. I think Options 1 is the
best choice.