2014-01-28 268 views
1

我有以下SQL存儲過程SQL CASE語句值

@Text varchar(max), 
@isArchived varchar(10) 

SELECT * 
    FROM [Table1] 
    WHERE [StringText] = @ AND [isArchived] = 

    (CASE 
     WHEN @isArchived = 'ALL' THEN ??? 
     WHEN @isArchived = 'Yes' THEN 1 
     WHEN @isArchived = 'No' THEN 0 
    END) 

我的問題是的isArchived是位類型的表中,並在網頁上我有對的isArchived三個值一個下拉列表:ALL,是的,

號爲Yes爲1(真),爲No爲0(假),但不知道要放什麼東西在這條線:WHEN @isArchived = 'ALL' THEN ???理想,將完全忽略該行AND [isArchived] = @isArchived但不確定它的語法是什麼。

回答

2

如果你想避免過濾時@isArchived = 'ALL',你可以比較[isArchived]本身(只要該值不能NULL):

SELECT * 
FROM [Table1] 
WHERE [StringText] = @Text AND [isArchived] = (
    CASE 
    WHEN @isArchived = 'ALL' THEN [isArchived] 
    WHEN @isArchived = 'Yes' THEN 1 
    WHEN @isArchived = 'No' THEN 0 
    END) 

注意,這種條件的過濾會影響您的執行計劃,所以你可能要在這篇文章中討論研究了不同的方法:

http://www.sommarskog.se/dyn-search-2008.html

+0

感謝。這樣做:)我認爲它應該沒問題,它將被放置在SSRS報告中。 –