I am developing a solution for statistic data for a customer and now i need to select some rows from a table (sell data) by using dynamic condition that come from another table (filled by the customer and it can change daily).
Also i need to use a cursor to perform more calculation and other stuff so this is the scenario:
DECLARE cRiga CURSOR LOCAL FAST_FORWARD FOR
-- here i will put the SELECT ...
OPEN cRiga
FETCH NEXT FROM cRiga INTO @field1, @field2, @field3, ...
WHILE @@FETCH_STATUS = 0
BEGIN
-- do some stuff here ...
FETCH NEXT FROM cRiga INTO @field1, @field2, @field3, ...
END
CLOSE cRiga
DEALLOCATE cRiga
this is the Condition Table where i can find the dynamic condition (see the colum Completa = 0)
IDIncentive CdMarca CdSettore CdGruppo Completa
----------- ------- --------- -------- -----------
1 COES NULL NULL 1
1 DELONG 10 0024 0 <
1 RHOSS NULL NULL 1
1 SILE 10 0012 0 <
1 SILE 11 0025 0 <
1 THERMI NULL NULL 1
....... more rows ...
To be more clear i try to explode the SQL required in many queries as:
Select Field1, ... from SELLDATA
where IDIncentive=1 and CdMarca='DELONG' and CdSettore=10 and CdGruppo='0024'
UNION ALL
Select Field1, ... from SELLDATA
where IDIncentive=1 and CdMarca='SILE' and CdSettore=10 and CdGruppo='0012'
UNION ALL
Select Field1, ... from SELLDATA
where IDIncentive=1 and CdMarca='SILE' and CdSettore=11 and CdGruppo='0025'
As u can imagine i cant do that because the conditions change daily by the customer so my question is:
How i can create a select that have all the conditions (as my example above) to use in the cursor ? There are a way ?
Thanks for who can help me and please advise if need more information to make more clear this question
You can use your already existing ConditionTable table to filter SELLDATA by means of join: no cursor, no union all.