I have a problem with my report from a live query. In my report i have 5 parameters that allow to pick a date range @DateFrom
and @DateTo
and 3 parameters which should allow to select specific attributes:
@salesid, @batch, @serial
I want to make date range parameters mandatory and that's working without any problems but last 3 parameters: @salesid
, @batch
, @serial
should be optional.
But they do not work as they should. Last 3 parameters should let you type in value which would work as filters. But when I pick date and one of those parameters Im getting the entire value of the query instead of selected values.
In parameters properties I slected "Allow blank value (" ")" option and just in case I defined a default value as blank.
That's how conditions in my query looks like:
where
st.custaccount <> 'number%'
and datepart(year,st.CREATEDDATETIME) >= 2012
and (ita.VENDORCODE like'producer1' or ita.vendorcode like 'producer2')
and st.createddatetime >= @FromDate
and st.createddatetime <= @ToDate
or (st.salesid = @salesid or @salesid is null)
or (itd.INVENTBATCHID = @batch or @batch is null)
or (itd.INVENTSERIALID = @serial or @serial is null)
Theoretically it should work but... Well but in practice it's not.
How to set a condition to get the desired effect? I couldn't find anything helpful so far. If any of you know something useful please give me some clues.
You need to change your final 3 or
statements to and
statements.
At the moment, your query is essentially checking for data items that match your criteria OR that any of those final three parameters is matched/null. This means that even if you data isn't the date range etc it will still be returned:
where st.custaccount <> 'number%'
and st.CREATEDDATETIME >= '20120101' -- Try not to use functions in your WHERE clause, as this stops indexes from working, making your query less efficient.
and ita.VENDORCODE in('producer1', 'producer2') -- IN has the same effect.
and st.createddatetime between @FromDate and @ToDate -- BETWEEN is an inclusive date range. Equivalent to >= and <=
and (st.salesid = @salesid
or @salesid is null
)
and (itd.INVENTBATCHID = @batch
or @batch is null
)
and (itd.INVENTSERIALID = @serial
or @serial is null
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.