Avatar of Jimbo99999
Jimbo99999
Flag for United States of America asked on

SQL Query variable in Where clause

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,
jimbo99999
Microsoft SQL ServerMicrosoft Server OSSSRSSQL

Avatar of undefined
Last Comment
Jimbo99999

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Dustin Saunders

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
NerdsOfTech

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Jimbo99999

ASKER
Thank you much appreciated.  I was not aware of this since being new to SSRS and teaching myself.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Jimbo99999

ASKER
Thank you Mark for your reference material links.
Jimbo99999

ASKER
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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes