SQL Query variable in Where clause

Jimbo99999 used Ask the Experts™
Good Day Experts!

I am hoping you can help me understand my options for resolving a dilemma.  I have developed a SQL query that runs fine and the desired output is achieved in ManagementStudio.
A parameter to the query(used to simulate User selection from an SSRS report) is a comma separated list.  Using that comma separated list,  I retrieve values from a table in my database and then build a string.  Here is the output of that string for the 4 comma separated values:

(incidentDate >= '2017-08-17' and incidentDate <= '2017-10-12') OR
(incidentDate >= '2017-10-16' and incidentDate <= '2017-12-19') OR
(incidentDate >= '2018-01-03' and incidentDate <= '2018-03-16') OR
(incidentDate >= '2018-03-19' and incidentDate <= '2018-05-31')

I set this to a variable to use in my query string which is then set to @cmdString. I then exec(@cmdString).  Works great no issues.

The issue I am having is when I try to use this in my SSRS report.  The editor does not recognize the fields to display on the report since they are in the string I am executing.  
Then when I try to create that query the "regular" way, I run into trouble because of trying to use a variable in the Where clause. I am not sure what to do.

Please ask questions as I probably did not explain well,
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Co-Founder and Chief Architect
Top Expert 2016
How about a stored procedure?  Then you can pass in the parameters to it.

There's a guide here ( http://www.sqlservercentral.com/articles/Reporting+Services+(SSRS)/117321/ )
NerdsOfTechTechnology Scientist
You can also make the query into a subquery, then using the WHERE clause of the outer query to filter using the alias of the column in reference.


Thank you much appreciated.  I was not aware of this since being new to SSRS and teaching myself.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Mark WillsTopic Advisor, Page Editor
Distinguished Expert 2018
Well, incidentdate will need to be part of your dataset so you can reference it.

While easiest is to use Stored Procedures for SSRS, it isnt the only method, but certainly a great place to start your SSRS career....

Here is some more reading for you - and if you start by having a quick look at step 4 then go back and read the rest of the Article : https://www.mssqltips.com/sqlservertip/2844/working-with-multiselect-parameters-for-ssrs-reports/

And, for perspective, then read http://www.sqlservercentral.com/articles/Report+Development/118315/

Admittedly, starting SSRS it can be confusing and just a bit frustrating. But hang in there, it does become easier :)


Thank you Mark for your reference material links.


Thank you all for your input.  I ended up using a table variable.  

Anytime, I receive help that I can put in my toolbox for later use, I like to acknowledge those who took the time to reply.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial