troubleshooting Question

Set variable in SQL Server query populated with list of strings

Avatar of Ike23
Ike23Flag for United States of America asked on
Microsoft SQL Server 2005Microsoft SQL Server 2008
7 Comments2 Solutions444 ViewsLast Modified:
I have a saved .sql script that I run a few different queries on using a list an IN() statement and a list of strings like below

--SET A VARIABLE FOR THE LIST OF STRINGS
DECLARE @QLIST varchar(2000) = 'STRING1','STRING2','STRING3','STRING4'


SELECT     ID
FROM      myTable1  
WHERE     qString IN (@QLIST)
ORDER BY PID

The problem I'm having is setting the actual @QLIST variable to be a comma delemited list with single quotes still surrounding each list item.

I searched around and didn't find what I'm looking for and thought one of the SQL experts here would have tried this before.

Thanks in advance!
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 7 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros