Subject Re: [firebird-support] Multiple WHERE clauses
Author Venus Software Operations

On 23/02/2015 01:27 pm, Svein Erling Tysvær svein.erling.tysvaer@... [firebird-support] wrote:
 

You may consider

WITH TMP(My72, cFrom, cTo, dValueFrom, dValueTo) as
(SELECT CAST(:My72 as integer), US1.dValue, US2. dValue FROM RDB$DATABASE
LEFT JOIN dSetValue('Current Year', 'From') US1 ON (1=1)
LEFT JOIN dSetValue('Current Year', 'To') US2 ON (1=1))
SELECT <whatever>
FROM TMP t
CROSS JOIN tDevelopment d
WHERE d.iID = t.My72 OR
(t.MyParam is null AND CAST(d.tDT AS DATE) BETWEEN t.dValueFrom, t.dValueTo)

I’ve assumed the dSetValue procedure returns 0 or 1 record. I did this because the way you wrote things, the procedure would be executed twice for every row in tDevelopment, something not necessary unless result of the procedure depends on values in tDevelopment (which it doesn’t in your case, since ‘Current Year’, ‘From’ and ‘To’ are constants).

HTH,
Set


Yes thanks a lot.  I have separated the initial SELECT I had will come back to the iID 72 later.  I did the following as a first:

WITH TMP(dFromDt, dToDt) as
(SELECT US1.dValue AS dFromDt, US2. dValue AS dToDt FROM RDB$DATABASE
LEFT JOIN dSetValue('Current Year', 'From') US1 ON (1=1)
LEFT JOIN dSetValue('Current Year', 'To') US2 ON (1=1))

SELECT ...
   FROM tDevelopment
   JOINs....
        JOIN Tmp
                ON CAST(tDevelopment.tDt AS DATE) BETWEEN Tmp.dFromDt AND Tmp.dToDt
...

That gives me the same result as I would other wise have had

Will come back to you and Sean soon.

Kind regards
Bhavbhuti