?
Solved

Parameter in where clause not working (where <column> in)

Posted on 2014-09-09
2
Medium Priority
?
189 Views
Last Modified: 2014-09-09
HI all,
I'm having an issue that is normally very simple; but I'm missing something here. I'm passing a simple comma dilimited string into a VarChar(MAX) parameter.  I'm then replacing the ',' with ''',''' in order to have the proper syntax to pass into the Where <colomn> not in @parameter.  

So I have a string:
AA660304-BE86-4C85-ABB6-EC62E15106D5,BC2B3858-CFD1-46E8-9E6D-F6E50DE4FADD

I obviously need to change the commas to include an appostraphe on iether side.
I can see that it does; but the parameter doesn't work.  If I do a select on the paramter after passed in I see that the syntax is correct; and when I use the resulting value of the parameter after the REPLACE FUNCTION, by copy/pasting it into the where clause it works.  

DECLARE @ExludeCategory VarChar(MAX) = 'AA660304-BE86-4C85-ABB6-EC62E15106D5,BC2B3858-CFD1-46E8-9E6D-F6E50DE4FADD'

SET @ExludeCategory = CHAR(39) + REPLACE(@ExludeCategory,',',''',''') + CHAR(39)
SELECT
      P.SADDR1            [Name]
      ,p.HMY
      
      ,SUM(CASE WHEN (W.DATE_Completion IS NULL) Then 1 ELSE 0 END) AS [Total Open]
            
FROM ORDERED W
      INNER JOIN PERSON P ON P.HMY = W.PHMY
      
WHERE (W.DATE_COMPLETION IS NULL)
      AND ((SELECT COUNT(*) FROM CATEGORY_ASSOC CA
                  WHERE (CA.WID = W.WID) AND (CA.CID NOT IN(@ExludeCategory))) > 0 )
      
GROUP BY P.SADDR1, P.HMY
ORDER BY P.SADDR1
0
Comment
Question by:Thunder724
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 25

Assisted Solution

by:Lee Savidge
Lee Savidge earned 1000 total points
ID: 40312399
You can't do that. You must use dynamic SQL.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 1000 total points
ID: 40312403
SQL will always consider the variable a single value in constructs like this:
CA.CID NOT IN(@ExludeCategory)

Use a splitter function to load the separate values into a table, then do a NOT EXISTS lookup on that table.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question