Subject Re: [firebird-support] Re: Possible to write this in a way that indices will be used
Author Ivan Prenosil
Maya,

> I'm <deep breath> going to dive in and try doing this regex stuff all inside the stored proc, executing
> it with EXECUTE STATEMENT.

If you plan to use EXECUTE STATEMENT, than IMHO it is much easier to construct
sql statement from individual pieces, than the other way, i.e. to break single
statement and remove unwanted parts. E.g. where clause for your original select

> select blah..,
> from MyTable D
> where ((D.SuplCde = :ISupplierCode) or (:ISupplierCode is null))
> and ((D.WrhseCde = :IWarehouseCode) or (:IWarehouseCode is null))
> and ((D.StkCde = :IStockCode) or (:IStockCode is null))
> and ((D.IsActve = 'Y') or (:IIncludeInactive = 'Y'))

can be "synthesized" quite easily this way


DECLARE VARIABLE CMD VARCHAR(1000);
...
CMD = SUBSTRING(
COALESCE(' AND D.SuplCde = ' || ISupplierCode, '') ||
COALESCE(' AND D.WrhseCde = ' || IWarehouseCode, '') ||
COALESCE(' AND D.StkCde = ' || IStockCode, '') ||
CASE WHEN IIncludeInactive = 'Y' THEN '' ELSE ' AND D.IsActve = 'Y'' END FROM 6);

IF (CMD<>'') THEN CMD = ' WHERE ' || CMD;

Ivan
http://www.volny.cz/iprenosil/interbase/