2

Here is a simple example - why does this work:

DECLARE @v as varchar(75)
SET @v = 'xxx-xxxx'
SELECT * FROM tbl_skus WHERE SKU = @v

But this does not work:

DECLARE @v as varchar(75)
SET @v = 'xxx-xxxx,yyy-yyyy'
SELECT * FROM tbl_skus WHERE SKU IN ( @v )

Both SKUs 'xxx-xxxx' and 'yyy-yyyy'

are in the table. The first query pulls 1 result, and the second pulls 0 results; no errors.

3 Answers 3

2

You cannot assign two values to a single variable. You will have to do something like this:

DECLARE @v as varchar(75) DECLARE @a as varchar(75)

SET @v = 'xxx-xxxx' SET @a = 'yyy-yyyy'

SELECT * FROM tbl_skus
WHERE SKU IN (@v, @a);
Sign up to request clarification or add additional context in comments.

Comments

1

The IN doesn't support passing in a list of values that way. Try it one of these two ways:

DECLARE @vList TABLE(v VARCHAR(75));

INSERT INTO @vList
VALUES('xxx-xxxx'),('yyy-yyyy');

SELECT * 
FROM tbl_skus 
WHERE SKU IN ( SELECT v FROM @vList)

Or you could:

SELECT *
FROM tbl_skus
WHERE SKU IN ('xxx-xxxx','yyy-yyyy')

Comments

0

First query eventually becomes SELECT * FROM tbl_skus WHERE SKU ='xxx-xxxx' , the second - SELECT * FROM tbl_skus WHERE SKU IN ('xxx-xxxx,yyy-yyyy') (1 value passed to IN - it searches for SKU ='xxx-xxxx,yyy-yyyy'); to return 2 rows it must look ... IN ('xxx-xxxx','yyy-yyyy') (2 values passed).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.