I am trying to produce a sql query based report that will allow a date range to be inserted in order to get a selection of the data available.
The problem I am having is that when I try this I get the error:
Must declare the scalar variable “@DateFrom”.
the sql i am using is below:
SELECT
[Cost Category ID]
FROM
SRVS.dbo.PACostCategories
WHERE
(@DateFrom IS NULL OR alias.myDate>= @DateFrom) AND
(@DateTo IS NULL OR alias.MyDate < DATEADD(d, 1, @DateTo))
Any pointers to get me in the right direction much appreciated.
You have to declare the variable before you try to run your query. You need to do the following to get this to work: