2013-10-09 130 views
14

是否可以使用的情況哪裏在子句? 事情是這樣的:SQL在WHERE IN子句中使用CASE語句

DECLARE @Status VARCHAR(50); 
SET @Status='published'; 

SELECT * FROM Product P  
WHERE P.Status IN (CASE WHEN @Status='published' THEN (1,3) 
            WHEN @Status='standby' THEN (2,5,9,6) 
            WHEN @Status='deleted' THEN (4,5,8,10) 
            ELSE (1,3) 
            END) 

此代碼提供了錯誤:附近有語法錯誤 ''。

+0

P.Status是什麼類型? –

+0

你可以在'where'中使用'case',但不是那樣。 'Case'必須爲每個語句返回一個值。 –

+0

@MaryamArshi:P.Status的類型是int。 – POIR

回答

20

不,你不能使用casein這樣。但是你可以做

SELECT * FROM Product P  
WHERE @Status='published' and P.Status IN (1,3) 
or @Status='standby' and P.Status IN (2,5,9,6) 
or @Status='deleted' and P.Status IN (4,5,8,10) 
or P.Status IN (1,3) 

BTW,你可以減少到

SELECT * FROM Product P  
WHERE @Status='standby' and P.Status IN (2,5,9,6) 
or @Status='deleted' and P.Status IN (4,5,8,10) 
or P.Status IN (1,3) 

因爲or P.Status IN (1,3)給你也這已經回答了@Status='published' and P.Status IN (1,3)

+2

你是否需要圍繞每個「或」子句使用括號?例如。或者(@ Status ='standby'和P.Status IN(2,5,9,6)) – dcp

+4

不要以爲你需要圓括號,但最後的'或'語句會返回錯誤的結果......看到這個小提琴 - 第2條記錄不應該返回。 http://sqlfiddle.com/#!3/9aef0/6 – sgeddes

9

我認識的所有記錄,但有一個輕微的問題與接受的解決方案。它會返回誤報。很容易解決:不需要

SELECT * FROM Products P  
WHERE (@Status='published' and P.Status IN (1,3)) 
    or (@Status='standby' and P.Status IN (2,5,9,6)) 
    or (@Status='deleted' and P.Status IN (4,5,8,10)) 
    or (@Status not in ('published','standby','deleted') and P.Status IN (1,2)) 

括號(儘管也許更容易,爲什麼我列入他們因此讀取)。

1
select * from Tran_LibraryBooksTrans LBT left join 
Tran_LibraryIssuedBooks LIB ON case WHEN LBT.IssuedTo='SN' AND 
LBT.LIBRARYTRANSID=LIB.LIBRARYTRANSID THEN 1 when LBT.IssuedTo='SM' 
AND LBT.LIBRARYTRANSID=LIB.LIBRARYTRANSID THEN 1 WHEN 
LBT.IssuedTo='BO' AND LBT.LIBRARYTRANSID=LIB.LIBRARYTRANSID THEN 1 
ELSE 0 END`enter code here`select * from Tran_LibraryBooksTrans LBT 
left join Tran_LibraryIssuedBooks LIB ON case WHEN LBT.IssuedTo='SN' 
AND LBT.LIBRARYTRANSID=LIB.LIBRARYTRANSID THEN 1 when 
LBT.IssuedTo='SM' AND LBT.LIBRARYTRANSID=LIB.LIBRARYTRANSID THEN 1 
WHEN LBT.IssuedTo='BO' AND LBT.LIBRARYTRANSID=LIB.LIBRARYTRANSID THEN 
1 ELSE 0 END 
1

SELECT * FROM Tran_LibraryBooksTrans LBT左加入 Tran_LibraryIssuedBooks LIB ON情況下,當LBT.IssuedTo = 'SN' AND LBT.LIBRARYTRANSID = LIB.LIBRARYTRANSID THEN 1時LBT.IssuedTo = 'SM' AND LBT.LIBRARYTRANSID = LIB.LIBRARYTRANSID THEN 1 WHEN LBT.IssuedTo = 'BO' AND LBT.LIBRARYTRANSID = LIB.LIBRARYTRANSID THEN 1 ELSE 0 END

1

也許你可以試試這個方法

SELECT * FROM Product P WHERE (CASE WHEN @Status = 'published' THEN (CASE WHEN P.Status IN (1, 3) THEN 'TRUE' ELSE FALSE END) WHEN @Status = 'standby' THEN (CASE WHEN P.Status IN (2, 5, 9, 6) THEN 'TRUE' ELSE 'FALSE' END) WHEN @Status = 'deleted' THEN (CASE WHEN P.Status IN (4, 5, 8, 10) THEN 'TRUE' ELSE 'FALSE' END) ELSE (CASE WHEN P.Status IN (1, 3) THEN 'TRUE' ELSE 'FALSE' END) END) = 'TRUE'

這樣,如果@Status =「出版」,查詢將檢查是否P.Status是其中1或3,它將返回TRUE,否則「假」。這將TRUE在年底

希望它有助於匹配。

1

我相信你可以在where子句中使用case語句,這裏是我如何做到這一點:

Select 
ProductID 
OrderNo, 
OrderType, 
OrderLineNo 
From Order_Detail 
Where ProductID in (
Select Case when (@Varibale1 != '') 
then (Select ProductID from Product P Where .......) 
Else (Select ProductID from Product) 
End as ProductID 
) 

這種方法已經爲我工作一次又一次。嘗試一下!